Her Cloud-to-cloud entegrasyonu, kullanıcıların kimliğini doğrulama mekanizması içermelidir.
Kimlik doğrulama, kullanıcılarınızın Google Hesaplarını kimlik doğrulama sisteminizdeki kullanıcı hesaplarıyla bağlamanıza olanak tanır. Bu sayede, karşılamanız bir akıllı ev amacı aldığında kullanıcılarınızı tanımlayabilirsiniz. Google akıllı ev yalnızca yetkilendirme kodu akışıyla OAuth'u destekler.
Bu sayfada, OAuth 2.0 sunucunuzu Cloud-to-cloud entegrasyonunuzla çalışacak şekilde nasıl ayarlayacağınız açıklanmaktadır.
OAuth ile Google Hesabı Bağlama
In the authorization code flow, you need two endpoints:
The authorization endpoint, which presents the sign-in UI to your users that aren't already signed in. The authorization endpoint also creates a short-lived authorization code to record users' consent to the requested access.
The token exchange endpoint, which is responsible for two types of exchanges:
- Exchanges an authorization code for a long-lived refresh token and a short-lived access token. This exchange happens when the user goes through the account linking flow.
- Exchanges a long-lived refresh token for a short-lived access token. This exchange happens when Google needs a new access token because the one it had expired.
Design guidelines
This section describes the design requirements and recommendations for the user screen that you host for OAuth linking flows. After it's called by Google's app, your platform displays a sign in to Google page and account linking consent screen to the user. The user is directed back to Google's app after giving their consent to link accounts.
Requirements
- You must communicate that the user’s account will be linked to Google, not a specific Google product like Google Home or Google Assistant.
- You must have a Google authorization statement such as "By signing in, you are authorizing Google to control your devices." See the Google Device Control Authorization section of the Google Home Developer Policies.
- You must open the Web OAuth linking page and ensure users have a clear method for signing in to their Google Account, such as fields for their username and password. Don't use the Google Sign-In (GSI) method that enables users to link without being taken to the Web OAuth Linking page. It is a violation of Google policy.
- You must include at least one of the following items in the OAuth linking
page to indicate the integration to which the user is linking:
- Company logo
- Company name
- Integration name
- App icon
Recommendations
We recommend that you do the following:
Display Google's Privacy Policy. Include a link to Google’s Privacy Policy on the consent screen.
Data to be shared. Use clear and concise language to tell the user what data of theirs Google requires and why.
Clear call-to-action. State a clear call-to-action on your consent screen, such as “Agree and link.” This is because users need to understand what data they're required to share with Google to link their accounts.
Ability to cancel. Provide a way for users to go back or cancel, if they choose not to link.
Clear sign-in process. Ensure that users have a clear method for signing in to their Google Account, such as fields for their username and password or Sign in with Google.
Ability to unlink. Offer a mechanism for users to unlink, such as a URL to their account settings on your platform. Alternatively, you can include a link to Google Account where users can manage their linked account.
Ability to change user account. Suggest a method for users to switch their account(s). This is especially beneficial if users tend to have multiple accounts.
- If a user must close the consent screen to switch accounts, send a recoverable error to Google so the user can sign in to the desired account with OAuth linking.
Include your logo. Display your company logo on the consent screen. Use your style guidelines to place your logo. If you wish to also display Google's logo, see Logos and trademarks.
Yetkilendirme kodu akışı
Yetkilendirme kodu akışının OAuth 2.0 sunucu uygulaması, hizmetinizin HTTPS üzerinden kullanılabilir hale getirdiği iki uç noktadan oluşur. İlk uç nokta, yetkilendirme uç noktasıdır. Bu uç nokta, veri erişimi için kullanıcılardan izin bulmaktan veya izin almaktan sorumludur. Yetkilendirme uç noktası, henüz oturum açmamış kullanıcılarınıza bir oturum açma kullanıcı arayüzü sunar ve istenen erişim için izni kaydeder. İkinci uç nokta, jeton değişimi uç noktasıdır. Bu uç nokta, kullanıcıya hizmetinize erişme yetkisi veren jeton adı verilen şifrelenmiş dizeleri almak için kullanılır.
Bir Google uygulamasının hizmetinizin API'lerinden birini çağırması gerektiğinde Google, kullanıcılarınızdan bu API'leri onlar adına çağırmak için izin almak üzere bu uç noktaları birlikte kullanır.
Google tarafından başlatılan bir OAuth 2.0 yetkilendirme kodu akışı oturumu aşağıdaki akışa sahiptir:
- Google, yetkilendirme uç noktanızı kullanıcının tarayıcısında açar. İşlem için akış yalnızca sesli cihazda başlatıldıysa Google, yürütmeyi telefona aktarır.
- Kullanıcı, henüz oturum açmadıysa oturum açar ve Google'a, daha önce izin vermediyse API'nizle verilerine erişme izni verir.
- Hizmetiniz bir yetkilendirme kodu oluşturur ve bunu Google'a döndürür. Bunu yapmak için kullanıcının tarayıcısını, isteğe eklenmiş yetkilendirme koduyla birlikte Google'a geri yönlendirin.
- Google, yetkilendirme kodunu jeton değişimi uç noktanıza gönderir. Bu uç nokta, kodun gerçekliğini doğrulayıp bir erişim jetonu ve bir yenileme jetonu döndürür. Erişim jetonu, hizmetinizin API'lere erişmek için kimlik bilgisi olarak kabul ettiği kısa ömürlü bir jetondur. Yenileme jetonu, Google'ın saklayabileceği ve süresi dolduğunda yeni erişim jetonları almak için kullanabileceği uzun ömürlü bir jetondur.
- Kullanıcı, hesap bağlama akışını tamamladıktan sonra Google'dan gönderilen her sonraki istekte bir erişim jetonu bulunur.
Yetkilendirme isteklerini işleme
OAuth 2.0 yetkilendirme kodu akışını kullanarak hesap bağlama işlemini gerçekleştirmeniz gerektiğinde Google, kullanıcıyı yetkilendirme uç noktanıza aşağıdaki parametreleri içeren bir istekle yönlendirir:
| Yetkilendirme uç noktası parametreleri | |
|---|---|
client_id |
Google'a atadığınız müşteri kimliği. |
redirect_uri |
Bu isteğe verilen yanıtı gönderdiğiniz URL. |
state |
Yönlendirme URI'sinde Google'a değiştirilmeden geri aktarılan bir muhasebe değeri. |
scope |
İsteğe bağlı: Google'ın yetkilendirme isteğinde bulunduğu verileri belirten, boşlukla ayrılmış bir kapsam dizeleri grubu. |
response_type |
Yanıt içinde döndürülecek değerin türü. OAuth 2.0
yetkilendirme kodu akışı için yanıt türü her zaman code olur.
|
Örneğin, yetkilendirme uç noktanız https://myservice.example.com/auth adresinde kullanılabiliyorsa istek şu şekilde görünebilir:
GET https://myservice.example.com/auth?client_id=GOOGLE_CLIENT_ID&redirect_uri=REDIRECT_URI&state=STATE_STRING&scope=REQUESTED_SCOPES&response_type=code
Yetkilendirme uç noktanızın oturum açma isteklerini işlemesi için aşağıdaki adımları uygulayın:
client_iddeğerinin Google'a atadığınız istemci kimliğiyle,redirect_urideğerinin ise Google tarafından hizmetiniz için sağlanan yönlendirme URL'siyle eşleştiğini doğrulayın. Bu kontroller, amaçlanmayan veya yanlış yapılandırılmış istemci uygulamalarına erişim verilmesini önlemek için önemlidir. Birden fazla OAuth 2.0 akışını destekliyorsanızresponse_typedeğerinincodeolduğunu da onaylayın.- Kullanıcının hizmetinizde oturum açıp açmadığını kontrol edin. Kullanıcı oturum açmamışsa hizmetinizin oturum açma veya kaydolma akışını tamamlayın.
- Google'ın API'nize erişmek için kullanacağı bir yetkilendirme kodu oluşturun. Yetkilendirme kodu herhangi bir dize değeri olabilir ancak kullanıcıyı, jetonun ait olduğu istemciyi ve kodun geçerlilik bitiş zamanını benzersiz bir şekilde temsil etmeli ve tahmin edilebilir olmamalıdır. Genellikle yaklaşık 10 dakika sonra geçerliliğini yitiren yetkilendirme kodları veriyorsunuz.
redirect_uriparametresiyle belirtilen URL'nin aşağıdaki biçimde olduğunu doğrulayın:https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
- Kullanıcının tarayıcısını
redirect_uriparametresiyle belirtilen URL'ye yönlendirin.codevestateparametrelerini ekleyerek yönlendirme yaptığınızda yeni oluşturduğunuz yetkilendirme kodunu ve orijinal, değiştirilmemiş durum değerini ekleyin. Aşağıda, sonuç URL'sine dair bir örnek verilmiştir:https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID?code=AUTHORIZATION_CODE&state=STATE_STRING
Jeton değişimi isteklerini işleme
Hizmetinizin jeton değişimi uç noktası iki tür jeton değişiminden sorumludur:
- Erişim jetonları ve yenileme jetonları için yetkilendirme kodlarını değiştirme
- Yenileme jetonlarını erişim jetonlarıyla değiştirme
Jeton değişimi istekleri aşağıdaki parametreleri içerir:
| Jeton değişimi uç nokta parametreleri | |
|---|---|
client_id |
İstek kaynağını Google olarak tanımlayan bir dize. Bu dize, sisteminizde Google'ın benzersiz tanımlayıcısı olarak kaydedilmelidir. |
client_secret |
Hizmetiniz için Google'a kaydettirdiğiniz gizli dize. |
grant_type |
Değiştirilen jetonun türü. authorization_code veya refresh_token olmalıdır. |
code |
grant_type=authorization_code olduğunda bu parametre, Google'ın oturum açma veya jeton değişimi uç noktanızdan aldığı koddur. |
redirect_uri |
grant_type=authorization_code olduğunda bu parametre, ilk yetkilendirme isteğinde kullanılan URL'dir. |
refresh_token |
grant_type=refresh_token olduğunda bu parametre, Google'ın jeton değişimi uç noktanızdan aldığı yenileme jetonudur. |
Google'ın sunucunuza kimlik bilgilerini nasıl göndereceğini yapılandırma
Yetkilendirme sunucunuz, uygulanma şekline bağlı olarak istemci kimlik bilgilerini istek gövdesinde veya istek başlığında almayı bekler.
Google, kimlik bilgilerini varsayılan olarak istek gövdesinde gönderir. Yetkilendirme sunucunuz, istemci kimlik bilgilerinin istek üstbilgisinde olmasını gerektiriyorsa Cloud-to-cloud entegrasyonunuzu buna göre yapılandırmanız gerekir:
Proje listesinde, üzerinde çalışmak istediğiniz projenin yanındaki Aç'ı tıklayın.
Cloud-to-Cloud bölümünde Develop'u (Geliştir) seçin.
Entegrasyonunuzun yanındaki Aç'ı tıklayın.
İzinler (isteğe bağlı) bölümüne gidin ve Google, istemci kimliğini ve gizliyi HTTP temel kimlik doğrulama üstbilgisi üzerinden iletsin onay kutusunu işaretleyin.
Değişikliklerinizi kaydetmek için Kaydet'i tıklayın.
Erişim jetonları ve yenileme jetonları için yetkilendirme kodlarını değiştirme
Kullanıcı oturum açtıktan ve yetkilendirme uç noktanız Google'a kısa ömürlü bir yetkilendirme kodu döndürdükten sonra Google, yetkilendirme kodunu erişim jetonu ve yenileme jetonu ile değiştirmek için jeton değişimi uç noktanıza bir istek gönderir.
Bu isteklerde grant_type değeri authorization_code, code değeri ise daha önce Google'a verdiğiniz yetkilendirme kodunun değeridir. Aşağıda, yetkilendirme kodunu erişim jetonu ve yenileme jetonuyla değiştirme isteğine ilişkin bir örnek verilmiştir:
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
Yetkilendirme kodlarını erişim jetonu ve yenileme jetonuyla değiştirmek için jeton değişimi uç noktanız, aşağıdaki adımları uygulayarak POST isteklerine yanıt verir:
client_iddeğerinin, isteğin kaynağını yetkili bir kaynak olarak tanımladığını veclient_secretdeğerinin beklenen değerle eşleştiğini doğrulayın.- Yetkilendirme kodunun geçerli ve süresinin dolmadığını, ayrıca istekte belirtilen istemci kimliğinin yetkilendirme koduyla ilişkili istemci kimliğiyle eşleştiğini doğrulayın.
redirect_uriparametresiyle belirtilen URL'nin, ilk yetkilendirme isteğinde kullanılan değerle aynı olduğunu doğrulayın.- Yukarıdaki ölçütlerin tümünü doğrulayamıyorsanız gövde olarak
{"error": "invalid_grant"}ile bir HTTP 400 Bad Request hatası döndürün. - Aksi takdirde, yenileme jetonu ve erişim jetonu oluşturmak için yetkilendirme kodundaki kullanıcı kimliğini kullanın. Bu jetonlar herhangi bir dize değeri olabilir ancak jetonun ait olduğu kullanıcıyı ve istemciyi benzersiz şekilde temsil etmeli ve tahmin edilebilir olmamalıdır. Erişim jetonları için jetonun geçerlilik bitiş zamanını da kaydedin. Bu süre genellikle jetonu yayınladıktan bir saat sonra sona erer. Yenileme jetonlarının süresi dolmaz.
- HTTPS yanıtının gövdesinde aşağıdaki JSON nesnesini döndürün:
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "refresh_token": "REFRESH_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
Google, kullanıcının erişim jetonunu ve yenileme jetonunu saklar ve erişim jetonunun geçerlilik bitimini kaydeder. Erişim jetonunun süresi dolduğunda Google, jeton değişimi uç noktanızdan yeni bir erişim jetonu almak için yenileme jetonunu kullanır.
Yenileme jetonlarını erişim jetonlarıyla değiştirme
Erişim jetonunun süresi dolduğunda Google, yenileme jetonunu yeni bir erişim jetonuyla değiştirmek için jeton değişimi uç noktanıza bir istek gönderir.
Bu isteklerde grant_type değeri refresh_token, refresh_token değeri ise daha önce Google'a verdiğiniz yenileme jetonunun değeridir. Aşağıda, yenileme jetonunu erişim jetonuyla değiştirme isteğine dair bir örnek verilmiştir:
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
Yenileme jetonunu erişim jetonuyla değiştirmek için jeton değişimi uç noktanız, POST isteklerine aşağıdaki adımları uygulayarak yanıt verir:
client_idparametresinin istek kaynağını Google olarak tanımladığını veclient_secretparametresinin beklenen değerle eşleştiğini doğrulayın.- Yenileme jetonunun geçerli olduğunu ve istekte belirtilen istemci kimliğinin, yenileme jetonuyla ilişkilendirilen istemci kimliğiyle eşleştiğini doğrulayın.
- Yukarıdaki tüm ölçütleri doğrulayamıyorsanız gövde olarak
{"error": "invalid_grant"}ile bir HTTP 400 Bad Request hatası döndürün. - Aksi takdirde, erişim jetonu oluşturmak için yeni jetonun kullanıcı kimliğini kullanın. Bu jetonlar herhangi bir dize değeri olabilir ancak jetonun ait olduğu kullanıcıyı ve istemciyi benzersiz bir şekilde temsil etmeli ve tahmin edilebilir olmamalıdır. Erişim jetonları için jetonun geçerlilik bitiş süresini de kaydedin. Bu süre genellikle jetonu yayınladıktan bir saat sonra sona erer.
- HTTPS yanıtının gövdesinde aşağıdaki JSON nesnesini döndürün:
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
Handle userinfo requests
The userinfo endpoint is an OAuth 2.0 protected resource that return claims about the linked user. Implementing and hosting the userinfo endpoint is optional, except for the following use cases:
- Linked Account Sign-In with Google One Tap.
- Frictionless subscription on AndroidTV.
After the access token has been successfully retrieved from your token endpoint, Google sends a request to your userinfo endpoint to retrieve basic profile information about the linked user.
| userinfo endpoint request headers | |
|---|---|
Authorization header |
The access token of type Bearer. |
For example, if your userinfo endpoint is available at
https://myservice.example.com/userinfo, a request might look like the following:
GET /userinfo HTTP/1.1 Host: myservice.example.com Authorization: Bearer ACCESS_TOKEN
For your userinfo endpoint to handle requests, do the following steps:
- Extract access token from the Authorization header and return information for the user associated with the access token.
- If the access token is invalid, return an HTTP 401 Unauthorized error with using the
WWW-AuthenticateResponse Header. Below is an example of a userinfo error response: If a 401 Unauthorized, or any other unsuccessful error response is returned during the linking process, the error will be non-recoverable, the retrieved token will be discarded and the user will have to initiate the linking process again.HTTP/1.1 401 Unauthorized WWW-Authenticate: error="invalid_token", error_description="The Access Token expired"
If the access token is valid, return and HTTP 200 response with the following JSON object in the body of the HTTPS response:
If your userinfo endpoint returns an HTTP 200 success response, the retrieved token and claims are registered against the user's Google account.{ "sub": "USER_UUID", "email": "EMAIL_ADDRESS", "given_name": "FIRST_NAME", "family_name": "LAST_NAME", "name": "FULL_NAME", "picture": "PROFILE_PICTURE", }userinfo endpoint response subA unique ID that identifies the user in your system. emailEmail address of the user. given_nameOptional: First name of the user. family_nameOptional: Last name of the user. nameOptional: Full name of the user. pictureOptional: Profile picture of the user.