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

শিল্প-মানসম্মত OAuth 2.0 অনুমোদন কোড প্রবাহ ব্যবহার করে অ্যাকাউন্টগুলো সংযুক্ত করা হয়।

এজেন্টদের জন্য OAuth 2.1 এবং PKCE

স্টেটলেস এআই এজেন্ট এবং মাল্টি-মোডাল পাইপলাইনের জন্য OAuth 2.1 প্রয়োগের সুপারিশ করা হয়।

  • PKCE (প্রুফ কী ফর কোড এক্সচেঞ্জ) : অনুমোদন কোডের প্রবাহ সুরক্ষিত করতে এবং আড়িপাতার আক্রমণ প্রতিরোধ করতে এটি অবশ্যই ব্যবহার করতে হবে।
  • অন্তর্নিহিত প্রবাহের অভাব : অন্তর্নিহিত প্রবাহ URL-এ অ্যাক্সেস টোকেন প্রকাশ করে, যা এজেন্ট পরিবেশের জন্য একটি নিরাপত্তা ঝুঁকি।

আপনার পরিষেবাতে অবশ্যই OAuth 2.0/2.1 অনুবর্তী অনুমোদন এবং টোকেন বিনিময় এন্ডপয়েন্ট সমর্থন থাকতে হবে।

Create the project

To create your project to use account linking:

  1. Go to the Google API Console.
  2. Click Create project.
  3. Enter a name or accept the generated suggestion.
  4. Confirm or edit any remaining fields.
  5. Click Create.

To view your project ID:

  1. Go to the Google API Console.
  2. Find your project in the table on the landing page. The project ID appears in the ID column.

The Google Account Linking process includes a consent screen which tells users the application requesting access to their data, what kind of data they are asking for and the terms that apply. You will need to configure your OAuth consent screen before generating a Google API client ID.

  1. Open the OAuth consent screen page of the Google APIs console.
  2. If prompted, select the project you just created.
  3. On the "OAuth consent screen" page, fill out the form and click the “Save” button.

    Application name: The name of the application asking for consent. The name should accurately reflect your application and be consistent with the application name users see elsewhere. The application name will be shown on the Account Linking consent screen.

    Application logo: An image on the consent screen that will help users recognize your app. The logo is shown on Account linking consent screen and on account settings

    Support email: For users to contact you with questions about their consent.

    Scopes for Google APIs: Scopes allow your application to access your user's private Google data. For the Google Account Linking use case, default scope (email, profile, openid) is sufficient, you don’t need to add any sensitive scopes. It is generally a best practice to request scopes incrementally, at the time access is required, rather than up front. Learn more.

    Authorized domains: To protect you and your users, Google only allows applications that authenticate using OAuth to use Authorized Domains. Your applications' links must be hosted on Authorized Domains. Learn more.

    Application Homepage link: Home page for your application. Must be hosted on an Authorized Domain.

    Application Privacy Policy link: Shown on Google Account Linking consent screen. Must be hosted on an Authorized Domain.

    Application Terms of Service link (Optional): Must be hosted on an Authorized Domain.

    Figure 1. Google Account Linking Consent Screen for a fictitious Application, Tunery

  4. Check "Verification Status", if your application needs verification then click the "Submit For Verification" button to submit your application for verification. Refer to OAuth verification requirements for details.

আপনার 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.

User Google App / Browser Google Server Your Auth Endpoint Your Token Endpoint 1. User initiates linking 2. Redirect to Auth Endpoint (GET) client_id, redirect_uri, state, scope 3. Display Sign-in & Consent Screen 4. User Authenticates & Grants Consent 5. Redirect back to Google (GET) code, state 6. Handle redirect & pass code/state 7. Token Exchange (POST) grant_type=authorization_code, code 8. Return Tokens (200 OK) access_token, refresh_token 9. Store user tokens 10. Access user resources
Figure 1. The sequence of events in the OAuth 2.0 Authorization Code flow for Google Account Linking.

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:

  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.

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:

  1. Validate the request:

    • Confirm that the client_id matches the Client ID assigned to Google.
    • Confirm that the redirect_uri matches 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_type is code.
  2. 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.
  3. 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.
  4. 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.

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.

  1. Validate the request:

    • Verify client_id and client_secret.
    • Verify the authorization code is valid and not expired.
    • Confirm redirect_uri matches the value used in Step 1.
    • If validation fails, return an HTTP 400 Bad Request with {"error": "invalid_grant"}.
  2. Issue tokens:

    • Generate a long-lived refresh_token and a short-lived access_token (typically 1 hour).
    • Return an HTTP 200 OK with the standard JSON token response.

B. Refresh access tokens

When the access token expires, Google requests a new one using the refresh token.

  1. Validate the request:

    • Verify client_id, client_secret, and refresh_token.
    • If validation fails, return an HTTP 400 Bad Request with {"error": "invalid_grant"}.
  2. Issue new access token:

    • Generate a new short-lived access_token.
    • Return an HTTP 200 OK with the JSON token response (optionally including a new refresh token).
ব্যবহারকারীর তথ্যের অনুরোধগুলি পরিচালনা করুন

ইউজারইনফো এন্ডপয়েন্ট হল একটি OAuth 2.0 সুরক্ষিত রিসোর্স যা লিঙ্ক করা ব্যবহারকারীর বিষয়ে দাবি ফেরত দেয়। ইউজার ইনফো এন্ডপয়েন্ট বাস্তবায়ন এবং হোস্ট করা ঐচ্ছিক, নিম্নলিখিত ব্যবহারের ক্ষেত্রে ছাড়া:

আপনার টোকেন এন্ডপয়েন্ট থেকে অ্যাক্সেস টোকেন সফলভাবে পুনরুদ্ধার করার পরে, Google লিঙ্ক করা ব্যবহারকারীর সম্পর্কে প্রাথমিক প্রোফাইল তথ্য পুনরুদ্ধার করার জন্য আপনার ব্যবহারকারীর তথ্য এন্ডপয়েন্টে একটি অনুরোধ পাঠায়।

userinfo এন্ডপয়েন্ট রিকোয়েস্ট হেডার
Authorization header টাইপ বিয়ারারের অ্যাক্সেস টোকেন।

উদাহরণস্বরূপ, যদি আপনার ব্যবহারকারীর তথ্যের এন্ডপয়েন্ট https://myservice.example.com/userinfo এ উপলব্ধ থাকে, তাহলে একটি অনুরোধ নিম্নলিখিতটির মতো দেখতে পারে:

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

অনুরোধগুলি পরিচালনা করার জন্য আপনার ব্যবহারকারীর তথ্যের শেষ পয়েন্টের জন্য, নিম্নলিখিত পদক্ষেপগুলি করুন:

  1. অনুমোদন শিরোনাম থেকে অ্যাক্সেস টোকেন বের করুন এবং অ্যাক্সেস টোকেনের সাথে যুক্ত ব্যবহারকারীর জন্য তথ্য ফেরত দিন।
  2. অ্যাক্সেস টোকেনটি অবৈধ হলে, WWW-Authenticate প্রতিক্রিয়া শিরোনাম ব্যবহার করে একটি HTTP 401 অননুমোদিত ত্রুটি ফেরত দিন। নীচে একটি ব্যবহারকারীর তথ্য ত্রুটি প্রতিক্রিয়ার একটি উদাহরণ:
    HTTP/1.1 401 Unauthorized
    WWW-Authenticate: error="invalid_token",
    error_description="The Access Token expired"
    
    লিঙ্কিং প্রক্রিয়া চলাকালীন যদি একটি 401 অননুমোদিত, বা অন্য কোনো অসফল ত্রুটির প্রতিক্রিয়া ফেরত দেওয়া হয়, তবে ত্রুটিটি পুনরুদ্ধারযোগ্য হবে না, পুনরুদ্ধার করা টোকেন বাতিল করা হবে এবং ব্যবহারকারীকে আবার লিঙ্কিং প্রক্রিয়া শুরু করতে হবে।
  3. অ্যাক্সেস টোকেনটি বৈধ হলে, HTTPS প্রতিক্রিয়ার বডিতে নিম্নলিখিত JSON অবজেক্টের সাথে HTTP 200 প্রতিক্রিয়া ফেরত দিন:

    {
    "sub": "USER_UUID",
    "email": "EMAIL_ADDRESS",
    "given_name": "FIRST_NAME",
    "family_name": "LAST_NAME",
    "name": "FULL_NAME",
    "picture": "PROFILE_PICTURE",
    }
    যদি আপনার ব্যবহারকারীর তথ্য এন্ডপয়েন্ট একটি HTTP 200 সাফল্যের প্রতিক্রিয়া প্রদান করে, তবে পুনরুদ্ধার করা টোকেন এবং দাবিগুলি ব্যবহারকারীর Google অ্যাকাউন্টের বিরুদ্ধে নিবন্ধিত হয়।

    ব্যবহারকারীর তথ্য শেষ পয়েন্ট প্রতিক্রিয়া
    sub একটি অনন্য আইডি যা আপনার সিস্টেমে ব্যবহারকারীকে শনাক্ত করে।
    email ব্যবহারকারীর ইমেল ঠিকানা।
    given_name ঐচ্ছিক: ব্যবহারকারীর প্রথম নাম।
    family_name ঐচ্ছিক: ব্যবহারকারীর শেষ নাম।
    name ঐচ্ছিক: ব্যবহারকারীর পুরো নাম।
    picture ঐচ্ছিক: ব্যবহারকারীর প্রোফাইল ছবি।

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

আপনি OAuth 2.0 প্লেগ্রাউন্ড টুলটি ব্যবহার করে আপনার বাস্তবায়ন যাচাই করতে পারেন।

টুলটিতে, নিম্নলিখিত ধাপগুলো অনুসরণ করুন:

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

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

টুলটিতে, নিম্নলিখিত ধাপগুলো অনুসরণ করুন:

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