Akun ditautkan menggunakan alur kode otorisasi OAuth 2.0 standar industri.
OAuth 2.1 &PKCE untuk Agen
Untuk agen AI tanpa status dan pipeline multi-modal, sebaiknya terapkan OAuth 2.1.
- PKCE (Proof Key for Code Exchange): Harus digunakan untuk mengamankan alur kode otorisasi, sehingga mencegah serangan intersepsi.
- Tidak Ada Alur Implisit: Alur implisit mengekspos token akses di URL, yang merupakan risiko keamanan untuk lingkungan agen.
Layanan Anda harus mendukung endpoint pertukaran token dan otorisasi yang sesuai dengan OAuth 2.0/2.1.
Membuat project
Untuk membuat project Anda agar dapat menggunakan penautan akun:
- Buka Konsol Google API.
- Klik Buat proyek.
- Masukkan nama atau terima saran yang dibuat.
- Konfirmasi atau edit kolom yang tersisa.
- Klik Buat.
Untuk melihat project ID Anda:
- Buka Konsol Google API.
- Cari project Anda di tabel pada halaman landing. Project ID muncul di kolom ID.
Mengonfigurasi Layar Izin OAuth Anda
Proses Penautan Akun Google mencakup layar izin yang memberi tahu pengguna aplikasi yang meminta akses ke data mereka, jenis data yang diminta, dan persyaratan yang berlaku. Anda harus mengonfigurasi layar izin OAuth sebelum membuat ID klien Google API.
- Buka halaman Layar izin OAuth di konsol API Google.
- Jika diminta, pilih project yang baru saja Anda buat.
Di halaman "Layar izin OAuth", isi formulir, lalu klik tombol “Simpan”.
Nama aplikasi: Nama aplikasi yang meminta izin. Nama harus mencerminkan aplikasi Anda secara akurat dan konsisten dengan nama aplikasi yang dilihat pengguna di tempat lain. Nama aplikasi akan ditampilkan di layar izin Penautan Akun.
Logo aplikasi: Gambar di layar izin yang akan membantu pengguna mengenali aplikasi Anda. Logo ditampilkan di layar izin Penautan akun dan di setelan akun
Email dukungan: Agar pengguna dapat menghubungi Anda jika ada pertanyaan tentang izin mereka.
Cakupan untuk Google API: Cakupan memungkinkan aplikasi Anda mengakses data pribadi Google pengguna Anda. Untuk kasus penggunaan Penautan Akun Google, cakupan default (email, profil, openid) sudah cukup, Anda tidak perlu menambahkan cakupan sensitif. Secara umum, praktik terbaiknya adalah meminta cakupan secara bertahap, pada saat akses diperlukan, bukan di awal. Pelajari lebih lanjut.
Domain yang diizinkan: Guna melindungi Anda dan pengguna Anda, Google hanya mengizinkan aplikasi yang melakukan autentikasi menggunakan OAuth untuk menggunakan Domain yang Diizinkan. Link aplikasi Anda harus dihosting di Domain yang Diotorisasi. Pelajari lebih lanjut.
Link Halaman Beranda Aplikasi: Halaman beranda untuk aplikasi Anda. Harus dihosting di Domain yang Diotorisasi.
Link Kebijakan Privasi Aplikasi: Ditampilkan di layar izin Penautan Akun Google. Harus dihosting di Domain yang Diotorisasi.
Link Persyaratan Layanan Aplikasi (Opsional): Harus dihosting di Domain yang Diotorisasi.
Gambar 1. Layar Izin Penautan Akun Google untuk Aplikasi fiktif, Tunery
Periksa "Status Verifikasi", jika aplikasi Anda memerlukan verifikasi, klik tombol "Kirim Untuk Verifikasi" untuk mengirimkan aplikasi Anda untuk diverifikasi. Lihat persyaratan verifikasi OAuth untuk mengetahui detailnya.
Mengimplementasikan server OAuth
An OAuth 2.0 server implementation of the authorization code flow consists of two endpoints, which your service makes available by HTTPS. The first endpoint is the authorization endpoint, which is responsible for finding or obtaining consent from users for data access. The authorization endpoint presents a sign-in UI to your users that aren't already signed in and records consent to the requested access. The second endpoint is the token exchange endpoint, which is used to obtain encrypted strings, called tokens, that authorize a user to access your service.
When a Google application needs to call one of your service's APIs, Google uses these endpoints together to get permission from your users to call these APIs on their behalf.
Google Account Linking: OAuth Authorization Code Flow
The following sequence diagram details interactions between the User, Google, and your service's endpoints.
Roles and responsibilities
The following table defines the roles and responsibilities of the actors in the Google Account Linking (GAL) OAuth flow. Note that in GAL, Google acts as the OAuth Client, while your service acts as the Identity/Service Provider.
| Actor / Component | GAL Role | Responsibilities |
|---|---|---|
| Google App / Server | OAuth Client | Initiates the flow, receives the authorization code, exchanges it for tokens, and securely stores them to access your service's APIs. |
| Your Authorization Endpoint | Authorization Server | Authenticates your users and obtains their consent to share access to their data with Google. |
| Your Token Exchange Endpoint | Authorization Server | Validates authorization codes and refresh tokens, and issues access tokens to the Google Server. |
| Google Redirect URI | Callback Endpoint | Receives the user redirect from your authorization service with the
code and state values. |
An OAuth 2.0 authorization code flow session initiated by Google has the following flow:
- Google opens your authorization endpoint in the user's browser. If the flow started on a voice-only device for an Action, Google transfers the execution to a phone.
- The user signs in, if not signed in already, and grants Google permission to access their data with your API, if they haven't already granted permission.
- Your service creates an authorization code and returns it to Google. To do so, redirect the user's browser back to Google with the authorization code attached to the request.
- Google sends the authorization code to your token exchange endpoint, which verifies the authenticity of the code and returns an access token and a refresh token. The access token is a short-lived token that your service accepts as credentials to access APIs. The refresh token is a long-lived token that Google can store and use to acquire new access tokens when they expire.
- After the user has completed the account linking flow, every subsequent request sent from Google contains an access token.
Implementation Recipe
Follow these steps to implement the Authorization Code flow.
Step 1: Handle authorization requests
When Google initiates account linking, it redirects the user to your authorization endpoint. For detailed protocol contracts and parameter requirements, see the Authorization Endpoint.
To handle the request, perform the following actions:
Validate the request:
- Confirm that the
client_idmatches the Client ID assigned to Google. - Confirm that the
redirect_urimatches the expected Google redirect URL:none https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID - Verify that
response_typeiscode.
- Confirm that the
Authenticate the user:
- Check if the user is signed in to your service.
- If the user is not signed in, prompt them to complete your sign-in or sign-up flow.
Generate authorization code:
- Create a unique, non-guessable authorization code associated with the user and client.
- Set the code to expire in approximately 10 minutes.
Redirect back to Google:
- Redirect the browser to the URL provided in
redirect_uri. - Append the following query parameters:
code: The authorization code you generated.state: The unmodified state value received from Google.
- Redirect the browser to the URL provided in
Step 2: Handle token exchange requests
Your token exchange endpoint processes two types of requests: exchanging codes for tokens, and refreshing expired access tokens. For detailed protocol contracts and parameter requirements, see the Token Exchange Endpoint.
A. Exchange authorization codes for tokens
When Google receives the authorization code, it calls your token exchange endpoint (POST) to retrieve tokens.
Validate the request:
- Verify
client_idandclient_secret. - Verify the authorization code is valid and not expired.
- Confirm
redirect_urimatches the value used in Step 1. - If validation fails, return an HTTP
400 Bad Requestwith{"error": "invalid_grant"}.
- Verify
Issue tokens:
- Generate a long-lived
refresh_tokenand a short-livedaccess_token(typically 1 hour). - Return an HTTP
200 OKwith the standard JSON token response.
- Generate a long-lived
B. Refresh access tokens
When the access token expires, Google requests a new one using the refresh token.
Validate the request:
- Verify
client_id,client_secret, andrefresh_token. - If validation fails, return an HTTP
400 Bad Requestwith{"error": "invalid_grant"}.
- Verify
Issue new access token:
- Generate a new short-lived
access_token. - Return an HTTP
200 OKwith the JSON token response (optionally including a new refresh token).
- Generate a new short-lived
Menangani permintaan info pengguna
Endpoint userinfo adalah resource yang dilindungi OAuth 2.0 yang menampilkan klaim tentang pengguna yang ditautkan. Menerapkan dan menghosting endpoint userinfo bersifat opsional, kecuali untuk kasus penggunaan berikut:
- Login dengan Akun Tertaut dengan Google One Ketuk.
- Langganan tanpa hambatan di Android TV.
Setelah token akses berhasil diambil dari endpoint token Anda, Google akan mengirimkan permintaan ke endpoint userinfo Anda untuk mengambil informasi profil dasar tentang pengguna yang ditautkan.
| header permintaan endpoint userinfo | |
|---|---|
Authorization header |
Token akses jenis Bearer. |
Misalnya, jika endpoint userinfo Anda tersedia di
https://myservice.example.com/userinfo, permintaan mungkin akan terlihat seperti berikut:
GET /userinfo HTTP/1.1 Host: myservice.example.com Authorization: Bearer ACCESS_TOKEN
Agar endpoint userinfo Anda dapat menangani permintaan, lakukan langkah-langkah berikut:
- Ekstrak token akses dari header Otorisasi dan tampilkan informasi untuk pengguna yang terkait dengan token akses.
- Jika token akses tidak valid, tampilkan error HTTP 401 Tidak Sah dengan menggunakan Header Respons
WWW-Authenticate. Berikut adalah contoh respons error userinfo: Jika pesan error 401 Tidak Sah, atau respons error lainnya yang tidak berhasil ditampilkan selama proses penautan, error tersebut tidak akan dapat dipulihkan, token yang diambil akan dihapus dan pengguna harus memulai proses penautan lagi.HTTP/1.1 401 Unauthorized WWW-Authenticate: error="invalid_token", error_description="The Access Token expired"
Jika token akses valid, tampilkan dan respons HTTP 200 dengan objek JSON berikut dalam isi HTTPS respons:
Jika endpoint userinfo Anda menampilkan respons sukses HTTP 200, token dan klaim yang diambil akan didaftarkan terhadap Akun Google pengguna.{ "sub": "USER_UUID", "email": "EMAIL_ADDRESS", "given_name": "FIRST_NAME", "family_name": "LAST_NAME", "name": "FULL_NAME", "picture": "PROFILE_PICTURE", }respons endpoint userinfo subID unik yang mengidentifikasi pengguna di sistem Anda. emailAlamat email pengguna. given_nameOpsional: Nama depan pengguna. family_nameOpsional: Nama belakang pengguna. nameOpsional: Nama lengkap pengguna. pictureOpsional: Foto profil pengguna.
Memvalidasi implementasi
Anda dapat memvalidasi penerapan dengan menggunakan alat OAuth 2.0 Playground.
Di alat, lakukan langkah-langkah berikut:
- Klik Konfigurasi untuk membuka jendela Konfigurasi OAuth 2.0.
- Di kolom OAuth flow, pilih Client-side.
- Di kolom OAuth Endpoints, pilih Custom.
- Tentukan endpoint OAuth 2.0 dan client ID yang Anda tetapkan ke Google di kolom yang sesuai.
- Di bagian Langkah 1, jangan pilih cakupan Google apa pun. Sebagai gantinya, biarkan kolom ini kosong atau ketik cakupan yang valid untuk server Anda (atau string arbitrer jika Anda tidak menggunakan cakupan OAuth). Setelah selesai, klik Izinkan API.
- Di bagian Langkah 2 dan Langkah 3, ikuti alur OAuth 2.0 dan verifikasi bahwa setiap langkah berfungsi sebagaimana mestinya.
Anda dapat memvalidasi penerapan dengan menggunakan alat Demo Penautan Akun Google.
Di alat, lakukan langkah-langkah berikut:
- Klik tombol Login dengan Google.
- Pilih akun yang ingin Anda tautkan.
- Masukkan ID layanan.
- Secara opsional, masukkan satu atau beberapa cakupan yang akan Anda minta aksesnya.
- Klik Mulai Demo.
- Jika diminta, konfirmasi bahwa Anda dapat menyetujui dan menolak permintaan penautan.
- Konfirmasi bahwa Anda dialihkan ke platform Anda.