החשבונות מקושרים באמצעות תהליכים מרומזים וקודי הרשאה של OAuth 2.0, בהתאם לתקנים המקובלים בתחום. השירות צריך לתמוך בנקודות קצה של הרשאה והחלפת אסימונים שתואמות ל-OAuth 2.0.
בתהליך העקיף, Google פותחת את נקודת הקצה (endpoint) של ההרשאה בדפדפן של המשתמש. אחרי הכניסה, מחזירים ל-Google אסימון גישה לטווח ארוך. טוקן הגישה הזה נכלל עכשיו בכל בקשה שנשלחת מ-Google.
בתהליך הרשאה באמצעות קוד, נדרשים שני נקודות קצה:
נקודת הקצה של ההרשאה, שבה מוצג ממשק המשתמש של הכניסה למשתמשים שעדיין לא מחוברים. נקודת הקצה להרשאה יוצרת גם קוד הרשאה לטווח קצר כדי לתעד את הסכמת המשתמשים לגישה המבוקשת.
נקודת הקצה החלפת אסימונים, שאחראית לשני סוגים של החלפות:
- מחליפה קוד הרשאה באסימון רענון לטווח ארוך ובאסימון גישה לטווח קצר. המרות כאלה מתרחשות כשהמשתמש עובר את תהליך קישור החשבון.
- המרת אסימון רענון לטווח ארוך באסימון גישה לטווח קצר. ההחלפה הזו מתרחשת כש-Google זקוקה לאסימון גישה חדש כי התוקף של האסימון הקודם פג.
בחירת תהליך OAuth 2.0
אמנם קל יותר להטמיע את התהליך המשתמע, אבל Google ממליצה שלא יהיה תאריך תפוגה לטוקני הגישה שהונפקו בתהליך המשתמע. הסיבה לכך היא שהמשתמש נדרש לקשר מחדש את החשבון שלו אחרי שתוקף האסימון פג בתהליך המשתמעות. אם אתם צריכים שתוקף האסימון יפוג, מומלץ מאוד להשתמש במקום זאת בתהליך של הרשאה לקוד.
הנחיות עיצוב
בקטע הזה מתוארות הדרישות וההמלצות לגבי העיצוב של מסך המשתמש שאתם מארחים בתהליכי הקישור של OAuth. אחרי שהאפליקציה של Google קוראת לאפליקציה, הפלטפורמה מציגה למשתמש מסך הסכמה לכניסה לדף של Google ולקישור חשבונות. המשתמש מופנה חזרה לאפליקציה של Google אחרי שהסכים לקישור החשבונות.

דרישות
- עליך לציין שחשבון המשתמש יקושר ל-Google, לא למוצר ספציפי של Google, כמו Google Home או Google Assistant.
המלצות
מומלץ לבצע את הפעולות הבאות:
הצגת מדיניות הפרטיות של Google צריך לכלול קישור למדיניות הפרטיות של Google במסך ההסכמה.
נתונים לשיתוף. חשוב להשתמש בשפה ברורה ותמציתית כדי להסביר למשתמשים אילו נתונים Google צריכה מהם ולמה.
קריאה ברורה לפעולה עליכם לציין במסך ההסכמה קריאה ברורה לפעולה, למשל 'מסכים/ה וקישור'. הסיבה לכך היא שהמשתמשים צריכים להבין אילו נתונים הם נדרשים לשתף עם Google כדי לקשר את החשבונות שלהם.
אפשרות לבטל צריך לספק למשתמשים אפשרות לחזור אחורה או לבטל, אם הם בוחרים לא לקשר.
תהליך כניסה נקי. חשוב לוודא שלמשתמשים יש שיטה ברורה לכניסה לחשבון Google, כמו שדות של שם המשתמש והסיסמה או כניסה באמצעות חשבון Google.
היכולת לבטל את הקישור. להציע למשתמשים מנגנון לביטול הקישור, כמו כתובת URL להגדרות החשבון שלהם בפלטפורמה שלכם. לחלופין, אפשר לכלול קישור לחשבון Google שבו המשתמשים יכולים לנהל את החשבון המקושר.
יכולת לשנות את חשבון המשתמש כדאי להציע למשתמשים שיטה להחלפת החשבונות. האפשרות הזו שימושית במיוחד אם למשתמשים יש כמה חשבונות.
- אם משתמש צריך לסגור את מסך ההסכמה כדי לעבור לחשבון אחר, צריך לשלוח ל-Google הודעת שגיאה שניתן לשחזור כדי שהמשתמש יוכל להיכנס לחשבון הרצוי באמצעות קישור OAuth והתהליך המשתמע.
מוסיפים את הלוגו שלכם. להציג את הלוגו של החברה במסך ההסכמה. השתמשו בהנחיות הסגנון שלכם כדי למקם את הלוגו. אם רוצים להציג גם את הלוגו של Google, אפשר לעיין במאמר סמלי לוגו וסימנים מסחריים.

יצירת הפרויקט
כדי ליצור את הפרויקט לשימוש בקישור לחשבון:
- לוחצים על יצירת פרויקט.
- מזינים שם או מאשרים את ההצעה שנוצרה.
- מאשרים או עורכים את השדות שנותרו.
- לוחצים על יצירה.
כדי לראות את מזהה הפרויקט:
- מחפשים את הפרויקט בטבלה בדף הנחיתה. מזהה הפרויקט מופיע בעמודה ID.
הגדרת מסך ההסכמה של OAuth
תהליך קישור חשבון Google כולל מסך הסכמה שבו מוצגת למשתמש האפליקציה שמבקשת גישה לנתונים שלו, סוג הנתונים שהיא מבקשת והתנאים הרלוונטיים. תצטרכו להגדיר את מסך ההסכמה ל-OAuth לפני שתפיקו מזהה לקוח של Google API.
- פותחים את הדף מסך ההסכמה ל-OAuth ב-Google APIs Console.
- אם מתבקשים, בוחרים את הפרויקט שיצרתם.
בדף 'מסך הסכמה ל-OAuth', ממלאים את הטופס ולוחצים על הלחצן 'שמירה'.
שם האפליקציה: שם האפליקציה שמבקשת הסכמה. השם צריך לשקף במדויק את האפליקציה שלכם ולהיות זהה לשם האפליקציה שמוצג למשתמשים במקומות אחרים. שם האפליקציה יוצג במסך ההסכמה לקישור החשבון.
הלוגו של האפליקציה: תמונה במסך ההסכמה שתעזור למשתמשים לזהות את האפליקציה שלכם. הלוגו מוצג במסך ההסכמה לקישור החשבון ובהגדרות החשבון
כתובת אימייל לתמיכה: כדי שהמשתמשים יוכלו ליצור איתכם קשר עם שאלות לגבי הסכמתם.
היקפים של ממשקי Google API: היקפים מאפשרים לאפליקציה שלכם לגשת לנתונים פרטיים של המשתמשים ב-Google. במקרה השימוש של קישור חשבון Google, היקף ברירת המחדל (אימייל, פרופיל, openid) מספיק, ואין צורך להוסיף היקפים רגישים. בדרך כלל מומלץ לבקש היקפי הרשאה באופן מצטבר, בזמן שנדרשת גישה, ולא מראש. מידע נוסף
דומיינים מורשים: כדי להגן עליכם ועל המשתמשים שלכם, Google מאפשרת רק לאפליקציות שעוברות אימות באמצעות OAuth להשתמש בדומיינים מורשים. הקישורים לאפליקציות שלכם צריכים להתארח בדומיינים מורשים. מידע נוסף
קישור לדף הבית של האפליקציה: דף הבית של האפליקציה. הן חייבות להתארח בדומיין מורשה.
קישור למדיניות הפרטיות של האפליקציה: מוצג במסך ההסכמה לקישור חשבון Google. הן חייבות להתארח בדומיין מורשה.
קישור לתנאים ולהגבלות של האפליקציה (אופציונלי): הקישור חייב להיות מאוחסן בדומיין מורשה.
איור 1. מסך הסכמה לקישור חשבון Google לאפליקציה פיקטיבית, Tunery
בודקים את 'סטטוס האימות'. אם האפליקציה שלכם צריכה אימות, לוחצים על הלחצן 'שליחה לאימות' כדי לשלוח את האפליקציה לאימות. פרטים נוספים זמינים במאמר בנושא דרישות האימות של OAuth.
הטמעת שרת 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.
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.
Handle authorization requests
When you need to perform account linking using the OAuth 2.0 authorization code flow, Google sends the user to your authorization endpoint with a request that includes the following parameters:
Authorization endpoint parameters | |
---|---|
client_id |
The Client ID you assigned to Google. |
redirect_uri |
The URL to which you send the response to this request. |
state |
A bookkeeping value that is passed back to Google unchanged in the redirect URI. |
scope |
Optional: A space-delimited set of scope strings that specify the data Google is requesting authorization for. |
response_type |
The type of value to return in the response. For the OAuth 2.0
authorization code flow, the response type is always code .
|
user_locale |
The Google Account language setting in RFC5646 format, used to localize your content in the user's preferred language. |
For example, if your authorization endpoint is available at
https://myservice.example.com/auth
, a request might look like the following:
GET https://myservice.example.com/auth?client_id=GOOGLE_CLIENT_ID&redirect_uri=REDIRECT_URI&state=STATE_STRING&scope=REQUESTED_SCOPES&response_type=code&user_locale=LOCALE
For your authorization endpoint to handle sign-in requests, do the following steps:
- Verify that the
client_id
matches the Client ID you assigned to Google, and that theredirect_uri
matches the redirect URL provided by Google for your service. These checks are important to prevent granting access to unintended or misconfigured client apps. If you support multiple OAuth 2.0 flows, also confirm that theresponse_type
iscode
. - Check if the user is signed in to your service. If the user isn't signed in, complete your service's sign-in or sign-up flow.
- Generate an authorization code for Google to use to access your API. The authorization code can be any string value, but it must uniquely represent the user, the client the token is for, and the code's expiration time, and it must not be guessable. You typically issue authorization codes that expire after approximately 10 minutes.
- Confirm that the URL specified by the
redirect_uri
parameter has the following form:https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
- Redirect the user's browser to the URL specified by the
redirect_uri
parameter. Include the authorization code you just generated and the original, unmodified state value when you redirect by appending thecode
andstate
parameters. The following is an example of the resulting URL:https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID?code=AUTHORIZATION_CODE&state=STATE_STRING
Handle token exchange requests
Your service's token exchange endpoint is responsible for two kinds of token exchanges:
- Exchange authorization codes for access tokens and refresh tokens
- Exchange refresh tokens for access tokens
Token exchange requests include the following parameters:
Token exchange endpoint parameters | |
---|---|
client_id |
A string that identifies the request origin as Google. This string must be registered within your system as Google's unique identifier. |
client_secret |
A secret string that you registered with Google for your service. |
grant_type |
The type of token being exchanged. It's either
authorization_code or refresh_token . |
code |
When grant_type=authorization_code , this parameter is the
code Google received from either your sign-in or token exchange
endpoint. |
redirect_uri |
When grant_type=authorization_code , this parameter is the
URL used in the initial authorization request. |
refresh_token |
When grant_type=refresh_token , this parameter is the
refresh token Google received from your token exchange endpoint. |
Exchange authorization codes for access tokens and refresh tokens
After the user signs in and your authorization endpoint returns a short-lived authorization code to Google, Google sends a request to your token exchange endpoint to exchange the authorization code for an access token and a refresh token.
For these requests, the value of grant_type
is authorization_code
, and the
value of code
is the value of the authorization code you previously granted
to Google. The following is an example of a request to exchange an
authorization code for an access token and a refresh token:
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded client_id=GOOGLE_CLIENT_ID&client_secret=GOOGLE_CLIENT_SECRET&grant_type=authorization_code&code=AUTHORIZATION_CODE&redirect_uri=REDIRECT_URI
To exchange authorization codes for an access token and a refresh token, your
token exchange endpoint responds to POST
requests by executing the following
steps:
- Verify that the
client_id
identifies the request origin as an authorized origin, and that theclient_secret
matches the expected value. - Verify that the authorization code is valid and not expired, and that the client ID specified in the request matches the client ID associated with the authorization code.
- Confirm that the URL specified by the
redirect_uri
parameter is identical to the value used in the initial authorization request. - If you can't verify all of the above criteria, return an HTTP
400 Bad Request error with
{"error": "invalid_grant"}
as the body. - Otherwise, use the user ID from the authorization code to generate a refresh token and an access token. These tokens can be any string value, but they must uniquely represent the user and the client the token is for, and they must not be guessable. For access tokens, also record the expiration time of the token, which is typically an hour after you issue the token. Refresh tokens don't expire.
- Return the following JSON object in the body of the HTTPS response:
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "refresh_token": "REFRESH_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
Google stores the access token and the refresh token for the user and records the expiration of the access token. When the access token expires, Google uses the refresh token to get a new access token from your token exchange endpoint.
Exchange refresh tokens for access tokens
When an access token expires, Google sends a request to your token exchange endpoint to exchange a refresh token for a new access token.
For these requests, the value of grant_type
is refresh_token
, and the value
of refresh_token
is the value of the refresh token you previously granted to
Google. The following is an example of a request to exchange a refresh token
for an access token:
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded client_id=GOOGLE_CLIENT_ID&client_secret=GOOGLE_CLIENT_SECRET&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
To exchange a refresh token for an access token, your token exchange endpoint
responds to POST
requests by executing the following steps:
- Verify that the
client_id
identifies the request origin as Google, and that theclient_secret
matches the expected value. - Verify that the refresh token is valid, and that the client ID specified in the request matches the client ID associated with the refresh token.
- If you can't verify all of the above criteria, return an HTTP 400
Bad Request error with
{"error": "invalid_grant"}
as the body. - Otherwise, use the user ID from the refresh token to generate an access token. These tokens can be any string value, but they must uniquely represent the user and the client the token is for, and they must not be guessable. For access tokens, also record the expiration time of the token, typically an hour after you issue the token.
- Return the following JSON object in the body of the HTTPS
response:
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
טיפול בבקשות למידע על משתמשים
נקודת הקצה של userinfo היא משאב מוגן ב-OAuth 2.0 שמחזיר תלונות לגבי המשתמש המקושר. ההטמעה והאירוח של נקודת הקצה של userinfo הם אופציונליים, חוץ מאשר בתרחישים הבאים לדוגמה:
- כניסה לחשבון המקושר באמצעות Google One Tap.
- מינוי פשוט וקל ב-AndroidTV.
אחרי שהאחזור של אסימון הגישה מנקודת הקצה של האסימון מתבצע, Google שולחת בקשה לנקודת הקצה (endpoint) של userinfo כדי לאחזר פרטי פרופיל בסיסיים של המשתמש המקושר.
כותרות של בקשות של נקודות קצה של userinfo | |
---|---|
Authorization header |
אסימון הגישה מסוג נושא. |
לדוגמה, אם נקודת הקצה של Userinfo זמינה
https://myservice.example.com/userinfo
, בקשה עשויה להיראות כך:
GET /userinfo HTTP/1.1 Host: myservice.example.com Authorization: Bearer ACCESS_TOKEN
כדי שנקודת הקצה של userinfo תטפל בבקשות, מבצעים את השלבים הבאים:
- מחלצים את אסימון הגישה מהכותרת Authorization ומחזירים מידע עבור המשתמש שמשויך לאסימון הגישה.
- אם אסימון הגישה לא חוקי, צריך להחזיר שגיאה מסוג HTTP 401 מאושר עם שימוש בכותרת התגובה
WWW-Authenticate
. דוגמה לתגובה עם שגיאה של userinfo: אם מתקבלת תגובה מסוג '401' ללא הרשאה, או כל תגובה אחרת של שגיאה שנכשלה במהלך תהליך הקישור, לא ניתן לשחזר את השגיאה, האסימון שאוחזר יימחק והמשתמש יצטרך להתחיל מחדש את תהליך הקישור.HTTP/1.1 401 Unauthorized WWW-Authenticate: error="invalid_token", error_description="The Access Token expired"
אם אסימון הגישה תקין, מוחזר ותגובת HTTP 200 עם אובייקט ה-JSON הבא בגוף ה-HTTPS תגובה:
אם נקודת הקצה (endpoint) של userinfo מחזירה תגובה מוצלחת מסוג HTTP 200, האסימון שאוחזר וההצהרות על זכויות יוצרים יירשמו בחשבון Google של המשתמש.{ "sub": "USER_UUID", "email": "EMAIL_ADDRESS", "given_name": "FIRST_NAME", "family_name": "LAST_NAME", "name": "FULL_NAME", "picture": "PROFILE_PICTURE", }
תגובה של נקודת הקצה של userinfo sub
מזהה ייחודי שמזהה את המשתמש במערכת שלכם. email
כתובת האימייל של המשתמש. given_name
אופציונלי: השם הפרטי של המשתמש. family_name
אופציונלי: שם המשפחה של המשתמש. name
אופציונלי: השם המלא של המשתמש. picture
אופציונלי: תמונת פרופיל של המשתמש.
אימות ההטמעה
אפשר לאמת את ההטמעה באמצעות הכלי OAuth 2.0 Playground.
בכלי, מבצעים את הפעולות הבאות:
- לוחצים על Configuration (הגדרה) כדי לפתוח את חלון ההגדרה של OAuth 2.0.
- בשדה תהליך OAuth, בוחרים באפשרות צד הלקוח.
- בשדה נקודות קצה של OAuth, בוחרים באפשרות בהתאמה אישית.
- מציינים את נקודת הקצה (endpoint) של OAuth 2.0 ואת מזהה הלקוח שהקציתם ל-Google בשדות המתאימים.
- בקטע שלב 1, לא בוחרים היקפי גישה של Google. במקום זאת, צריך להשאיר את השדה הזה ריק או להקליד היקף תקין לשרת (או מחרוזת שרירותית אם לא משתמשים בהיקפים של OAuth). בסיום, לוחצים על Authorize API.
- בקטעים שלב 2 ושלב 3, מבצעים את התהליך OAuth 2.0 ומוודאים שכל שלב פועל כמו שצריך.
אפשר לאמת את ההטמעה באמצעות הכלי הדגמה של קישור לחשבון Google.
בכלי, מבצעים את השלבים הבאים:
- לוחצים על הלחצן כניסה באמצעות חשבון Google.
- בוחרים את החשבון שרוצים לקשר.
- מזינים את מזהה השירות.
- אפשר להזין היקף אחד או יותר שלגביו רוצים לבקש גישה.
- לוחצים על התחלת הדגמה.
- כשמופיעה הבקשה, מאשרים את האפשרות להביע הסכמה לדחיית בקשת הקישור.
- מוודאים שאתם מופנים מחדש לפלטפורמה שלכם.