OAuth এর সাথে Google অ্যাকাউন্ট লিঙ্ক করা

অ্যাকাউন্টগুলি ইন্ডাস্ট্রি স্ট্যান্ডার্ড OAuth 2.0 অন্তর্নিহিত এবং অনুমোদন কোড ফ্লো ব্যবহার করে লিঙ্ক করা হয়। আপনার পরিষেবা অবশ্যই OAuth 2.0 অনুগত অনুমোদন এবং টোকেন বিনিময় শেষ পয়েন্ট সমর্থন করবে৷

অন্তর্নিহিত প্রবাহে, Google ব্যবহারকারীর ব্রাউজারে আপনার অনুমোদনের শেষ পয়েন্ট খোলে। সফল সাইন ইন করার পরে, আপনি Google-এ একটি দীর্ঘস্থায়ী অ্যাক্সেস টোকেন ফেরত দেন। এই অ্যাক্সেস টোকেনটি এখন Google থেকে পাঠানো প্রতিটি অনুরোধে অন্তর্ভুক্ত করা হয়েছে।

অনুমোদন কোড প্রবাহে, আপনার দুটি শেষ পয়েন্ট প্রয়োজন:

  • অনুমোদন এন্ডপয়েন্ট, যা আপনার ব্যবহারকারীদের সাইন-ইন UI উপস্থাপন করে যারা ইতিমধ্যে সাইন ইন করেনি। অনুমোদনের শেষ পয়েন্টটি অনুরোধ করা অ্যাক্সেসে ব্যবহারকারীদের সম্মতি রেকর্ড করার জন্য একটি স্বল্প-কালীন অনুমোদন কোডও তৈরি করে।

  • টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট, যা দুই ধরনের এক্সচেঞ্জের জন্য দায়ী:

    1. একটি দীর্ঘস্থায়ী রিফ্রেশ টোকেন এবং একটি স্বল্পকালীন অ্যাক্সেস টোকেনের জন্য একটি অনুমোদন কোড বিনিময় করে৷ এই বিনিময়টি ঘটে যখন ব্যবহারকারী অ্যাকাউন্ট লিঙ্কিং প্রবাহের মধ্য দিয়ে যায়।
    2. একটি স্বল্পকালীন অ্যাক্সেস টোকেনের জন্য দীর্ঘস্থায়ী রিফ্রেশ টোকেন বিনিময় করে। Google এর একটি নতুন অ্যাক্সেস টোকেনের প্রয়োজন হলে এই বিনিময়টি ঘটে কারণ এটির মেয়াদ শেষ হয়ে গেছে।

একটি OAuth 2.0 ফ্লো বেছে নিন

যদিও অন্তর্নিহিত প্রবাহ প্রয়োগ করা সহজ, Google সুপারিশ করে যে অন্তর্নিহিত প্রবাহ দ্বারা জারি করা অ্যাক্সেস টোকেন কখনই মেয়াদ শেষ হবে না। এর কারণ হল একটি টোকেন অন্তর্নিহিত প্রবাহের সাথে মেয়াদ শেষ হওয়ার পরে ব্যবহারকারীকে তাদের অ্যাকাউন্ট আবার লিঙ্ক করতে বাধ্য করা হয়। নিরাপত্তার কারণে আপনার টোকেনের মেয়াদ শেষ হওয়ার প্রয়োজন হলে, আমরা দৃঢ়ভাবে সুপারিশ করছি যে আপনি পরিবর্তে অনুমোদন কোড প্রবাহ ব্যবহার করুন।

ডিজাইন নির্দেশিকা

এই বিভাগে আপনি OAuth লিঙ্কিং ফ্লোগুলির জন্য হোস্ট করা ব্যবহারকারী স্ক্রিনের জন্য ডিজাইনের প্রয়োজনীয়তা এবং সুপারিশগুলি বর্ণনা করে৷ Google-এর অ্যাপ দ্বারা এটিকে কল করার পরে, আপনার প্ল্যাটফর্মটি Google পৃষ্ঠায় একটি সাইন ইন এবং ব্যবহারকারীকে অ্যাকাউন্ট লিঙ্ক করার সম্মতি স্ক্রীন প্রদর্শন করে। অ্যাকাউন্ট লিঙ্ক করতে তাদের সম্মতি দেওয়ার পরে ব্যবহারকারীকে Google-এর অ্যাপে ফেরত পাঠানো হয়।

এই চিত্রটি একটি ব্যবহারকারীর জন্য তাদের Google অ্যাকাউন্টকে আপনার প্রমাণীকরণ সিস্টেমের সাথে লিঙ্ক করার পদক্ষেপগুলি দেখায়৷ প্রথম স্ক্রিনশটটি আপনার প্ল্যাটফর্ম থেকে ব্যবহারকারীর সূচনা লিঙ্ক দেখায়। দ্বিতীয় চিত্রটি ব্যবহারকারীর Google-এ সাইন-ইন দেখায়, যখন তৃতীয়টি আপনার অ্যাপের সাথে তাদের Google অ্যাকাউন্ট লিঙ্ক করার জন্য ব্যবহারকারীর সম্মতি এবং নিশ্চিতকরণ দেখায়। চূড়ান্ত স্ক্রিনশট Google অ্যাপে একটি সফলভাবে লিঙ্ক করা ব্যবহারকারীর অ্যাকাউন্ট দেখায়।
চিত্র 1. অ্যাকাউন্ট লিঙ্ককারী ব্যবহারকারী Google-এ সাইন ইন করুন এবং সম্মতি স্ক্রীন।

প্রয়োজনীয়তা

  1. আপনাকে অবশ্যই যোগাযোগ করতে হবে যে ব্যবহারকারীর অ্যাকাউন্টটি Google-এর সাথে লিঙ্ক করা হবে, Google Home বা Google Assistant-এর মতো নির্দিষ্ট Google পণ্যের সাথে নয়

সুপারিশ

আমরা আপনাকে নিম্নলিখিতগুলি করার পরামর্শ দিই:

  1. Google এর গোপনীয়তা নীতি প্রদর্শন করুন। সম্মতি স্ক্রিনে Google-এর গোপনীয়তা নীতির একটি লিঙ্ক অন্তর্ভুক্ত করুন।

  2. ডেটা শেয়ার করতে হবে। ব্যবহারকারীকে তাদের Google-এর কোন ডেটা প্রয়োজন এবং কেন তা জানাতে স্পষ্ট এবং সংক্ষিপ্ত ভাষা ব্যবহার করুন৷

  3. কল-টু-অ্যাকশন পরিষ্কার করুন। আপনার সম্মতি স্ক্রিনে একটি স্পষ্ট কল-টু-অ্যাকশন বলুন, যেমন "সম্মতি দিন এবং লিঙ্ক করুন।" এর কারণ হল ব্যবহারকারীদের বুঝতে হবে তাদের অ্যাকাউন্ট লিঙ্ক করার জন্য Google-এর সাথে কী ডেটা শেয়ার করতে হবে।

  4. বাতিল করার ক্ষমতা। ব্যবহারকারীরা লিঙ্ক না করা বেছে নিলে তাদের ফিরে যেতে বা বাতিল করার একটি উপায় প্রদান করুন।

  5. সাইন-ইন প্রক্রিয়া পরিষ্কার করুন। নিশ্চিত করুন যে ব্যবহারকারীদের তাদের Google অ্যাকাউন্টে সাইন ইন করার জন্য পরিষ্কার পদ্ধতি আছে, যেমন তাদের ব্যবহারকারীর নাম এবং পাসওয়ার্ডের জন্য ক্ষেত্র বা Google দিয়ে সাইন ইন করুন

  6. লিঙ্কমুক্ত করার ক্ষমতা। ব্যবহারকারীদের আনলিঙ্ক করার জন্য একটি প্রক্রিয়া অফার করুন, যেমন আপনার প্ল্যাটফর্মে তাদের অ্যাকাউন্ট সেটিংসের URL। বিকল্পভাবে, আপনি Google অ্যাকাউন্টে একটি লিঙ্ক অন্তর্ভুক্ত করতে পারেন যেখানে ব্যবহারকারীরা তাদের লিঙ্ক করা অ্যাকাউন্ট পরিচালনা করতে পারে।

  7. ব্যবহারকারীর অ্যাকাউন্ট পরিবর্তন করার ক্ষমতা। ব্যবহারকারীদের তাদের অ্যাকাউন্ট(গুলি) পরিবর্তন করার জন্য একটি পদ্ধতির পরামর্শ দিন। এটি বিশেষত উপকারী যদি ব্যবহারকারীদের একাধিক অ্যাকাউন্ট থাকে।

    • যদি কোনো ব্যবহারকারীকে অ্যাকাউন্ট পাল্টানোর জন্য সম্মতি স্ক্রীন বন্ধ করতে হয়, Google-এ একটি পুনরুদ্ধারযোগ্য ত্রুটি পাঠান যাতে ব্যবহারকারী OAuth লিঙ্কিং এবং অন্তর্নিহিত প্রবাহের মাধ্যমে পছন্দসই অ্যাকাউন্টে সাইন ইন করতে পারেন।
  8. আপনার লোগো অন্তর্ভুক্ত করুন. সম্মতি স্ক্রিনে আপনার কোম্পানির লোগো দেখান। আপনার লোগো স্থাপন করতে আপনার শৈলী নির্দেশিকা ব্যবহার করুন. আপনি যদি Google-এর লোগোও প্রদর্শন করতে চান, লোগো এবং ট্রেডমার্ক দেখুন।

创建项目

如需创建项目以使用帐号关联,请按以下步骤操作:

  1. Go to the Google API Console.
  2. প্রকল্প তৈরি করুন ক্লিক করুন
  3. একটি নাম লিখুন বা উত্পন্ন পরামর্শ গ্রহণ করুন।
  4. নিশ্চিত করুন বা কোনও অবশিষ্ট ক্ষেত্র সম্পাদনা করুন।
  5. তৈরি ক্লিক করুন

আপনার প্রকল্পের আইডি দেখতে:

  1. Go to the Google API Console.
  2. অবতরণ পৃষ্ঠায় সারণীতে আপনার প্রকল্পটি সন্ধান করুন। প্রকল্প আইডি আইডি কলামে প্রদর্শিত হবে।

Google 帐号关联流程包括一个同意屏幕,用于告知用户请求访问其数据的应用、用户要求的数据类型以及适用的条款。您需要先配置 OAuth 权限请求页面,然后才能生成 Google API 客户端 ID。

  1. 打开 Google API 控制台的 OAuth 同意屏幕页面。
  2. 如果出现提示,请选择您刚刚创建的项目。
  3. 在“OAuth 同意屏幕”页面上,填写表单,然后点击“保存”按钮。

    应用名称:请求用户同意的应用的名称。该名称应准确反映您的应用,并与用户在别处看到的应用名称保持一致。应用名称将显示在帐号关联同意屏幕上。

    应用徽标:同意屏幕上的图片,有助于用户识别您的应用。徽标会显示在帐号关联同意屏幕和帐号设置

    支持电子邮件地址:供用户就其同意情况与您联系。

    Google API 的范围:范围允许您的应用访问用户的私有 Google 数据。对于 Google 帐号关联用例,默认范围(电子邮件、个人资料、OpenID)就足够了,您无需添加任何敏感范围。最佳做法一般是在需要访问时逐步请求作用域,而不是预先请求。了解详情

    已获授权的网域:为保护您和您的用户,Google 仅允许使用 OAuth 进行身份验证的应用使用已获授权的网域。您应用的链接必须托管在已获授权的网域上。了解详情

    应用首页链接:您的应用的首页。必须托管在已获授权的网域上。

    应用隐私权政策链接:在 Google 帐号关联同意屏幕上显示。必须托管在已获授权的网域上。

    应用服务条款链接(可选):必须托管在已获授权的网域上。

    图 1. 一款虚构应用 Tunery 的 Google 帐号关联同意屏幕

  4. 查看“验证状态”。如果您的申请需要验证,请点击“提交验证”按钮,提交您的申请。如需了解详情,请参阅 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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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:

  1. Verify that the client_id matches the Client ID you assigned to Google, and that the redirect_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 the response_type is code.
  2. 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.
  3. 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.
  4. 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
      
  5. 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 the code and state 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:

  1. Verify that the client_id identifies the request origin as an authorized origin, and that the client_secret matches the expected value.
  2. 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.
  3. Confirm that the URL specified by the redirect_uri parameter is identical to the value used in the initial authorization request.
  4. If you can't verify all of the above criteria, return an HTTP 400 Bad Request error with {"error": "invalid_grant"} as the body.
  5. 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.
  6. 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:

  1. Verify that the client_id identifies the request origin as Google, and that the client_secret matches the expected value.
  2. 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.
  3. If you can't verify all of the above criteria, return an HTTP 400 Bad Request error with {"error": "invalid_grant"} as the body.
  4. 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.
  5. Return the following JSON object in the body of the HTTPS response:
    {
    "token_type": "Bearer",
    "access_token": "ACCESS_TOKEN",
    "expires_in": SECONDS_TO_EXPIRATION
    }
处理用户信息请求

用户信息终端是一个OAuth 2.0保护的资源,对链接的用户返回的权利要求。实现和托管 userinfo 端点是可选的,以下用例除外:

从您的令牌端点成功检索访问令牌后,Google 会向您的 userinfo 端点发送请求,以检索有关链接用户的基本个人资料信息。

userinfo 端点请求标头
Authorization header Bearer 类型的访问令牌。

例如,如果你的用户信息终端可在https://myservice.example.com/userinfo ,请求看起来像下面这样:

GET /userinfo HTTP/1.1
Host: myservice.example.com
Authorization: Bearer ACCESS_TOKEN

要让您的 userinfo 端点处理请求,请执行以下步骤:

  1. 从 Authorization 标头中提取访问令牌并返回与访问令牌关联的用户的信息。
  2. 如果访问令牌无效,返回HTTP 401错误未经授权使用的WWW-Authenticate响应头。下面是一个userinfo的错误响应的一个示例:
    HTTP/1.1 401 Unauthorized
    WWW-Authenticate: error="invalid_token",
    error_description="The Access Token expired"
    
    如果一个401未经授权,或任何其它不成功错误响应在关联过程返回时,误差将是不可恢复的,所检索的令牌将被丢弃,并且用户将必须再次启动链接过程。
  3. 如果访问令牌是有效的,回国与以下JSON对象在HTTPS响应的身体HTTP 200回应:

    {
    "sub": "USER_UUID",
    "email": "EMAIL_ADDRESS",
    "given_name": "FIRST_NAME",
    "family_name": "LAST_NAME",
    "name": "FULL_NAME",
    "picture": "PROFILE_PICTURE",
    }
    
    如果你的用户信息端点返回一个HTTP 200成功响应,检索到的令牌和索赔登记针对用户的谷歌帐户。

    用户信息端点响应
    sub标识系统中用户的唯一 ID。
    email用户的电子邮件地址。
    given_name可选:用户的名字。
    family_name可选:用户的姓氏。
    name可选:用户的全名。
    picture可选:用户的档案图片。

আপনার বাস্তবায়ন যাচাই করা হচ্ছে

আপনি ব্যবহার করে আপনার বাস্তবায়ন যাচাই করতে পারেন OAuth এর 2.0 খেলার মাঠ হাতিয়ার।

সরঞ্জামটিতে, নিম্নলিখিত পদক্ষেপগুলি করুন:

  1. কনফিগারেশন ক্লিক করুন OAuth এর 2.0 কনফিগারেশন উইন্ডোটি বন্ধ করুন।
  2. OAuth এর প্রবাহ ক্ষেত্রে, ক্লায়েন্ট-সাইড নির্বাচন করুন।
  3. OAuth এর এন্ডপয়েন্ট ক্ষেত্রে, কাস্টম নির্বাচন করুন।
  4. সংশ্লিষ্ট ক্ষেত্রগুলিতে আপনার OAuth 2.0 এন্ডপয়েন্ট এবং Google- কে আপনার দেওয়া ক্লায়েন্ট আইডি উল্লেখ করুন।
  5. ধাপ 1 বিভাগে, যেকোনো Google সুযোগ নির্বাচন করবেন না। পরিবর্তে, এই ক্ষেত্রটি ফাঁকা রাখুন অথবা আপনার সার্ভারের জন্য বৈধ একটি সুযোগ টাইপ করুন (অথবা যদি আপনি OAuth স্কোপ ব্যবহার না করেন তবে একটি নির্বিচারে স্ট্রিং)। আপনার হয়ে গেলে, অনুমোদন API গুলি ক্লিক করুন।
  6. ধাপ 2 এবং 3 য় ধাপ বিভাগগুলিতে, OAuth এর 2.0 প্রবাহ মধ্য দিয়ে যেতে এবং যাচাই করুন যে প্রতিটি পদক্ষেপ হিসাবে উদ্দীষ্ট কাজ করে।

আপনি ব্যবহার করে আপনার বাস্তবায়ন যাচাই করতে পারেন Google অ্যাকাউন্টের সাথে লিঙ্ক ডেমো হাতিয়ার।

সরঞ্জামটিতে, নিম্নলিখিত পদক্ষেপগুলি করুন:

  1. ক্লিক Google বাটন সঙ্গে সাইন-ইন।
  2. আপনি যে অ্যাকাউন্টটি লিঙ্ক করতে চান তা চয়ন করুন।
  3. পরিষেবা আইডি লিখুন।
  4. Allyচ্ছিকভাবে এক বা একাধিক ক্ষেত্র লিখুন যার জন্য আপনি অ্যাক্সেসের অনুরোধ করবেন।
  5. স্টার্ট ডেমো ক্লিক করুন।
  6. যখন অনুরোধ করা হবে, নিশ্চিত করুন যে আপনি সম্মতি দিতে পারেন এবং লিঙ্কিং অনুরোধটি অস্বীকার করতে পারেন।
  7. নিশ্চিত করুন যে আপনি আপনার প্ল্যাটফর্মে পুনirectনির্দেশিত হয়েছেন।