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

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

In the implicit flow, Google opens your authorization endpoint in the user's browser. After successful sign in, you return a long-lived access token to Google. This access token is now included in every request sent from Google.

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:

    1. 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.
    2. 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.

Choose an OAuth 2.0 flow

Although the implicit flow is simpler to implement, Google recommends that access tokens issued by the implicit flow never expire. This is because the user is forced to link their account again after a token expires with the implicit flow. If you need token expiration for security reasons, we strongly recommend that you use the authorization code flow instead.

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.

This figure shows the steps for a user to link their Google account
            to your authentication system. The first screenshot shows
            user-initiated linking from your platform. The second image shows
            user sign-in to Google, while the third shows the user consent and
            confirmation for linking their Google account with your app. The
            final screenshot shows a successfully linked user account in the
            Google app.
Figure 1. Account linking user sign in to Google and consent screens.

Requirements

  1. You must communicate that the user’s account will be linked to Google, not a specific Google product like Google Home or Google Assistant.

Recommendations

We recommend that you do the following:

  1. Display Google's Privacy Policy. Include a link to Google’s Privacy Policy on the consent screen.

  2. Data to be shared. Use clear and concise language to tell the user what data of theirs Google requires and why.

  3. 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.

  4. Ability to cancel. Provide a way for users to go back or cancel, if they choose not to link.

  5. Clear sign-in process. Ensure that users have clear method for signing in to their Google account, such as fields for their username and password or Sign in with Google.

  6. 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.

  7. 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 and the implicit flow.
  8. 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.

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 সার্ভার বাস্তবায়ন করুন

অনুমোদন কোড প্রবাহের একটি OAuth 2.0 সার্ভার বাস্তবায়নে দুটি শেষ পয়েন্ট থাকে, যা আপনার পরিষেবা HTTPS দ্বারা উপলব্ধ করে। প্রথম এন্ডপয়েন্ট হল অনুমোদনের এন্ডপয়েন্ট, যা ডেটা অ্যাক্সেসের জন্য ব্যবহারকারীদের কাছ থেকে সম্মতি খোঁজার বা পাওয়ার জন্য দায়ী। অনুমোদনের শেষ পয়েন্ট আপনার ব্যবহারকারীদের কাছে একটি সাইন-ইন UI উপস্থাপন করে যেগুলি ইতিমধ্যে সাইন ইন করেনি এবং অনুরোধ করা অ্যাক্সেসের সম্মতি রেকর্ড করে৷ দ্বিতীয় এন্ডপয়েন্ট হল টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট, যা এনক্রিপ্ট করা স্ট্রিং পেতে ব্যবহৃত হয়, যাকে টোকেন বলা হয়, যা একজন ব্যবহারকারীকে আপনার পরিষেবা অ্যাক্সেস করার অনুমতি দেয়।

যখন একটি Google অ্যাপ্লিকেশনকে আপনার পরিষেবার APIগুলির মধ্যে একটিতে কল করার প্রয়োজন হয়, তখন Google তাদের পক্ষে এই APIগুলিকে কল করার জন্য আপনার ব্যবহারকারীদের কাছ থেকে অনুমতি পেতে এই শেষ পয়েন্টগুলি একসাথে ব্যবহার করে৷

Google দ্বারা শুরু করা একটি OAuth 2.0 অনুমোদন কোড ফ্লো সেশনের নিম্নলিখিত প্রবাহ রয়েছে:

  1. Google ব্যবহারকারীর ব্রাউজারে আপনার অনুমোদনের শেষ পয়েন্ট খোলে। যদি কোনো অ্যাকশনের জন্য শুধুমাত্র ভয়েস ডিভাইসে প্রবাহ শুরু হয়, তাহলে Google এক্সিকিউশনটিকে একটি ফোনে স্থানান্তর করে।
  2. ব্যবহারকারী সাইন ইন করে, যদি ইতিমধ্যে সাইন ইন না করে থাকে, এবং যদি তারা ইতিমধ্যে অনুমতি না দেয় তবে আপনার API দিয়ে তাদের ডেটা অ্যাক্সেস করার জন্য Google-কে অনুমতি দেয়৷
  3. আপনার পরিষেবা একটি অনুমোদন কোড তৈরি করে এবং এটি Google-এ ফেরত দেয়। এটি করতে, অনুরোধের সাথে সংযুক্ত অনুমোদন কোড সহ ব্যবহারকারীর ব্রাউজারটিকে Google-এ ফেরত পাঠান৷
  4. Google আপনার টোকেন এক্সচেঞ্জ এন্ডপয়েন্টে অনুমোদন কোড পাঠায়, যা কোডের সত্যতা যাচাই করে এবং একটি অ্যাক্সেস টোকেন এবং একটি রিফ্রেশ টোকেন ফেরত দেয়। অ্যাক্সেস টোকেন হল একটি স্বল্পস্থায়ী টোকেন যা আপনার পরিষেবা API অ্যাক্সেস করার জন্য শংসাপত্র হিসাবে গ্রহণ করে। রিফ্রেশ টোকেন হল একটি দীর্ঘস্থায়ী টোকেন যা Google সংরক্ষণ করতে পারে এবং মেয়াদ শেষ হয়ে গেলে নতুন অ্যাক্সেস টোকেনগুলি অর্জন করতে ব্যবহার করতে পারে৷
  5. ব্যবহারকারী অ্যাকাউন্ট লিঙ্কিং ফ্লো সম্পূর্ণ করার পরে, Google থেকে পাঠানো প্রতিটি পরবর্তী অনুরোধে একটি অ্যাক্সেস টোকেন থাকে।

অনুমোদনের অনুরোধগুলি পরিচালনা করুন

যখন আপনাকে OAuth 2.0 অনুমোদন কোড ফ্লো ব্যবহার করে অ্যাকাউন্ট লিঙ্ক করার প্রয়োজন হয়, তখন Google নিম্নলিখিত প্যারামিটারগুলি অন্তর্ভুক্ত করে এমন একটি অনুরোধ সহ ব্যবহারকারীকে আপনার অনুমোদনের শেষ পয়েন্টে পাঠায়:

অনুমোদনের শেষ পয়েন্ট পরামিতি
client_id আপনি Google-এ যে ক্লায়েন্ট আইডি অ্যাসাইন করেছেন।
redirect_uri আপনি যে URLটিতে এই অনুরোধের প্রতিক্রিয়া পাঠান।
state পুনঃনির্দেশ URI-তে অপরিবর্তিত Google-এ ফেরত পাঠানো হয়।
scope ঐচ্ছিক: স্কোপ স্ট্রিংগুলির একটি স্পেস-ডিলিমিটেড সেট যা Google যে ডেটার জন্য অনুমোদনের অনুরোধ করছে তা নির্দিষ্ট করে৷
response_type প্রতিক্রিয়াতে যে মানের প্রত্যাবর্তন করতে হবে। OAuth 2.0 অনুমোদন কোড প্রবাহের জন্য, প্রতিক্রিয়ার ধরনটি সর্বদাই code
user_locale RFC5646 ফর্ম্যাটে Google অ্যাকাউন্টের ভাষা সেটিং, ব্যবহারকারীর পছন্দের ভাষায় আপনার বিষয়বস্তু স্থানীয়করণ করতে ব্যবহৃত হয়।

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

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

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

  1. যাচাই করুন যে client_id আপনার Google-এ নির্ধারিত ক্লায়েন্ট আইডির সাথে মেলে এবং আপনার পরিষেবার জন্য Google দ্বারা প্রদত্ত পুনঃনির্দেশ URL এর সাথে redirect_uri মেলে। অনাকাঙ্ক্ষিত বা ভুল কনফিগার করা ক্লায়েন্ট অ্যাপগুলিতে অ্যাক্সেস মঞ্জুর করা প্রতিরোধ করার জন্য এই চেকগুলি গুরুত্বপূর্ণ৷ আপনি যদি একাধিক OAuth 2.0 ফ্লো সমর্থন করেন, তাও নিশ্চিত করুন যে response_type code
  2. ব্যবহারকারী আপনার পরিষেবাতে সাইন ইন করেছেন কিনা তা পরীক্ষা করুন৷ ব্যবহারকারী সাইন ইন না করলে, আপনার পরিষেবার সাইন-ইন বা সাইন-আপ প্রবাহ সম্পূর্ণ করুন৷
  3. আপনার API অ্যাক্সেস করার জন্য Google ব্যবহার করার জন্য একটি অনুমোদন কোড তৈরি করুন। অনুমোদন কোড যেকোন স্ট্রিং মান হতে পারে, তবে এটি অবশ্যই ব্যবহারকারীকে অনন্যভাবে উপস্থাপন করতে হবে, টোকেনটি যে ক্লায়েন্টের জন্য এবং কোডের মেয়াদ শেষ হওয়ার সময়, এবং এটি অনুমানযোগ্য হতে হবে না। আপনি সাধারণত অনুমোদন কোড ইস্যু করেন যা প্রায় 10 মিনিটের পরে মেয়াদ শেষ হয়ে যায়।
  4. নিশ্চিত করুন যে redirect_uri প্যারামিটার দ্বারা নির্দিষ্ট করা URL-এর নিম্নলিখিত ফর্ম রয়েছে:
      https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID
      https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
      
  5. redirect_uri প্যারামিটার দ্বারা নির্দিষ্ট URL-এ ব্যবহারকারীর ব্রাউজারকে পুনঃনির্দেশ করুন। code এবং state প্যারামিটার যোগ করে রিডাইরেক্ট করার সময় আপনি এইমাত্র তৈরি করা অনুমোদন কোড এবং আসল, অপরিবর্তিত রাষ্ট্রীয় মান অন্তর্ভুক্ত করুন। নিম্নলিখিত ফলাফল URL এর একটি উদাহরণ:
    https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID?code=AUTHORIZATION_CODE&state=STATE_STRING

টোকেন বিনিময় অনুরোধ হ্যান্ডেল

আপনার পরিষেবার টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট দুটি ধরণের টোকেন এক্সচেঞ্জের জন্য দায়ী:

  • অ্যাক্সেস টোকেন এবং রিফ্রেশ টোকেনগুলির জন্য অনুমোদনের কোডগুলি বিনিময় করুন৷
  • অ্যাক্সেস টোকেনগুলির জন্য রিফ্রেশ টোকেনগুলি বিনিময় করুন৷

টোকেন বিনিময় অনুরোধ নিম্নলিখিত পরামিতি অন্তর্ভুক্ত:

টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট প্যারামিটার
client_id একটি স্ট্রিং যা অনুরোধের উত্সটিকে Google হিসাবে চিহ্নিত করে৷ এই স্ট্রিংটি অবশ্যই আপনার সিস্টেমের মধ্যে Google এর অনন্য শনাক্তকারী হিসাবে নিবন্ধিত হতে হবে৷
client_secret একটি গোপন স্ট্রিং যা আপনি আপনার পরিষেবার জন্য Google এর সাথে নিবন্ধন করেছেন৷
grant_type টোকেনের ধরন বিনিময় হচ্ছে। এটি হয় authorization_code বা refresh_token
code যখন grant_type=authorization_code , এই প্যারামিটারটি হল Google আপনার সাইন-ইন বা টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট থেকে প্রাপ্ত কোড।
redirect_uri grant_type=authorization_code হলে, এই প্যারামিটারটি প্রাথমিক অনুমোদনের অনুরোধে ব্যবহৃত URL।
refresh_token যখন grant_type=refresh_token , এই প্যারামিটারটি হল রিফ্রেশ টোকেন Google আপনার টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট থেকে প্রাপ্ত।
অ্যাক্সেস টোকেন এবং রিফ্রেশ টোকেনগুলির জন্য অনুমোদনের কোডগুলি বিনিময় করুন৷

ব্যবহারকারী সাইন ইন করার পরে এবং আপনার অনুমোদনের এন্ডপয়েন্ট Google-কে একটি স্বল্পকালীন অনুমোদনের কোড ফেরত দেওয়ার পরে, Google আপনার টোকেন এক্সচেঞ্জ এন্ডপয়েন্টে একটি অ্যাক্সেস টোকেন এবং একটি রিফ্রেশ টোকেনের জন্য অনুমোদন কোড বিনিময় করার জন্য একটি অনুরোধ পাঠায়।

এই অনুরোধগুলির জন্য, grant_type এর মান হল authorization_code , এবং code মান হল অনুমোদন কোডের মান যা আপনি আগে Google-কে দিয়েছিলেন৷ একটি অ্যাক্সেস টোকেন এবং একটি রিফ্রেশ টোকেনের জন্য একটি অনুমোদন কোড বিনিময় করার অনুরোধের একটি উদাহরণ নিম্নলিখিত:

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

একটি অ্যাক্সেস টোকেন এবং একটি রিফ্রেশ টোকেনের জন্য অনুমোদনের কোডগুলি বিনিময় করতে, আপনার টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট নিম্নলিখিত পদক্ষেপগুলি সম্পাদন করে POST অনুরোধে সাড়া দেয়:

  1. যাচাই করুন যে client_id অনুরোধের উত্সটিকে একটি অনুমোদিত উত্স হিসাবে চিহ্নিত করে এবং client_secret প্রত্যাশিত মানের সাথে মেলে৷
  2. যাচাই করুন যে অনুমোদন কোডটি বৈধ এবং মেয়াদ শেষ হয়নি এবং অনুরোধে উল্লেখিত ক্লায়েন্ট আইডি অনুমোদন কোডের সাথে যুক্ত ক্লায়েন্ট আইডির সাথে মেলে।
  3. নিশ্চিত করুন যে redirect_uri প্যারামিটার দ্বারা নির্দিষ্ট করা URL প্রাথমিক অনুমোদনের অনুরোধে ব্যবহৃত মানের সাথে অভিন্ন।
  4. আপনি যদি উপরের সমস্ত মানদণ্ড যাচাই করতে না পারেন, তাহলে একটি HTTP 400 খারাপ অনুরোধের ত্রুটি {"error": "invalid_grant"} দিয়ে বডি হিসাবে ফেরত দিন।
  5. অন্যথায়, একটি রিফ্রেশ টোকেন এবং একটি অ্যাক্সেস টোকেন তৈরি করতে অনুমোদন কোড থেকে ব্যবহারকারী আইডি ব্যবহার করুন৷ এই টোকেনগুলি যে কোনও স্ট্রিং মান হতে পারে, তবে সেগুলি অবশ্যই ব্যবহারকারী এবং ক্লায়েন্টকে অনন্যভাবে উপস্থাপন করতে হবে যার জন্য টোকেনটি রয়েছে এবং সেগুলি অনুমানযোগ্য হতে হবে না। অ্যাক্সেস টোকেনগুলির জন্য, টোকেনের মেয়াদ শেষ হওয়ার সময়ও রেকর্ড করুন, যা সাধারণত আপনি টোকেন ইস্যু করার এক ঘন্টা পরে। রিফ্রেশ টোকেন মেয়াদ শেষ হয় না.
  6. HTTPS প্রতিক্রিয়ার মূল অংশে নিম্নলিখিত JSON অবজেক্টটি ফেরত দিন:
    {
    "token_type": "Bearer",
    "access_token": "ACCESS_TOKEN",
    "refresh_token": "REFRESH_TOKEN",
    "expires_in": SECONDS_TO_EXPIRATION
    }

Google ব্যবহারকারীর জন্য অ্যাক্সেস টোকেন এবং রিফ্রেশ টোকেন সংরক্ষণ করে এবং অ্যাক্সেস টোকেনের মেয়াদ শেষ হওয়ার রেকর্ড করে। অ্যাক্সেস টোকেনের মেয়াদ শেষ হয়ে গেলে, Google আপনার টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট থেকে একটি নতুন অ্যাক্সেস টোকেন পেতে রিফ্রেশ টোকেন ব্যবহার করে।

অ্যাক্সেস টোকেনগুলির জন্য রিফ্রেশ টোকেনগুলি বিনিময় করুন৷

যখন একটি অ্যাক্সেস টোকেনের মেয়াদ শেষ হয়ে যায়, তখন Google একটি নতুন অ্যাক্সেস টোকেনের জন্য একটি রিফ্রেশ টোকেন বিনিময় করার জন্য আপনার টোকেন এক্সচেঞ্জ এন্ডপয়েন্টে একটি অনুরোধ পাঠায়।

এই অনুরোধগুলির জন্য, grant_type এর মান হল refresh_token , এবং refresh_token এর মান হল রিফ্রেশ টোকেনের মান যা আপনি আগে Google-কে দিয়েছিলেন৷ নিম্নলিখিতটি একটি অ্যাক্সেস টোকেনের জন্য একটি রিফ্রেশ টোকেন বিনিময় করার অনুরোধের একটি উদাহরণ:

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

একটি অ্যাক্সেস টোকেনের জন্য একটি রিফ্রেশ টোকেন বিনিময় করতে, আপনার টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট নিম্নলিখিত পদক্ষেপগুলি সম্পাদন করে POST অনুরোধে সাড়া দেয়:

  1. যাচাই করুন যে client_id অনুরোধের উৎসটিকে Google হিসাবে চিহ্নিত করে এবং client_secret প্রত্যাশিত মানের সাথে মেলে।
  2. রিফ্রেশ টোকেন বৈধ কিনা যাচাই করুন এবং অনুরোধে উল্লেখিত ক্লায়েন্ট আইডি রিফ্রেশ টোকেনের সাথে যুক্ত ক্লায়েন্ট আইডির সাথে মেলে।
  3. আপনি যদি উপরের সমস্ত মানদণ্ড যাচাই করতে না পারেন, তাহলে একটি HTTP 400 খারাপ অনুরোধের ত্রুটি {"error": "invalid_grant"} দিয়ে বডি হিসাবে ফেরত দিন।
  4. অন্যথায়, একটি অ্যাক্সেস টোকেন তৈরি করতে রিফ্রেশ টোকেন থেকে ব্যবহারকারী আইডি ব্যবহার করুন। এই টোকেনগুলি যে কোনও স্ট্রিং মান হতে পারে, তবে সেগুলি অবশ্যই ব্যবহারকারী এবং ক্লায়েন্টকে অনন্যভাবে উপস্থাপন করতে হবে যার জন্য টোকেনটি রয়েছে এবং সেগুলি অনুমানযোগ্য হতে হবে না। অ্যাক্সেস টোকেনগুলির জন্য, টোকেনের মেয়াদ শেষ হওয়ার সময়ও রেকর্ড করুন, সাধারণত আপনি টোকেন ইস্যু করার এক ঘন্টা পরে।
  5. HTTPS প্রতিক্রিয়ার মূল অংশে নিম্নলিখিত JSON অবজেক্টটি ফেরত দিন:
    {
    "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:

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:

  1. Extract access token from the Authorization header and return information for the user associated with the access token.
  2. If the access token is invalid, return an HTTP 401 Unauthorized error with using the WWW-Authenticate Response Header. Below is an example of a userinfo error response:
    HTTP/1.1 401 Unauthorized
    WWW-Authenticate: error="invalid_token",
    error_description="The Access Token expired"
    
    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.
  3. If the access token is valid, return and HTTP 200 response with the following JSON object in the body of the HTTPS response:

    {
    "sub": "USER_UUID",
    "email": "EMAIL_ADDRESS",
    "given_name": "FIRST_NAME",
    "family_name": "LAST_NAME",
    "name": "FULL_NAME",
    "picture": "PROFILE_PICTURE",
    }
    If your userinfo endpoint returns an HTTP 200 success response, the retrieved token and claims are registered against the user's Google account.

    userinfo endpoint response
    sub A unique ID that identifies the user in your system.
    email Email address of the user.
    given_name Optional: First name of the user.
    family_name Optional: Last name of the user.
    name Optional: Full name of the user.
    picture Optional: Profile picture of the user.

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

您可以使用 OAuth 2.0 Playground 工具验证您的实现。

在该工具中,执行以下步骤:

  1. 点击配置 以打开 OAuth 2.0 配置窗口。
  2. OAuth flow 字段中,选择 Client-side(客户端)。
  3. OAuth 端点字段中,选择自定义
  4. 在相应字段中指定您的 OAuth 2.0 端点和您分配给 Google 的客户端 ID。
  5. 第 1 步部分,不要选择任何 Google 范围。请将此字段留空或输入对服务器有效的范围(如果您不使用 OAuth 范围,则可以输入任意字符串)。完成后,点击授权 API
  6. Step 2Step 3 部分中,完成 OAuth 2.0 流程,并验证每个步骤是否按预期运行。

您可以使用 Google 账号关联演示版工具验证您的实现。

在该工具中,执行以下步骤:

  1. 点击使用 Google 账号登录按钮。
  2. 选择您要关联的账号。
  3. 输入服务 ID。
  4. (可选)输入您要请求访问权限的一个或多个范围。
  5. 点击开始演示
  6. 当系统提示时,请确认您同意或拒绝关联请求。
  7. 确认您已被重定向到您的平台。
,

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

In the implicit flow, Google opens your authorization endpoint in the user's browser. After successful sign in, you return a long-lived access token to Google. This access token is now included in every request sent from Google.

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:

    1. 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.
    2. 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.

Choose an OAuth 2.0 flow

Although the implicit flow is simpler to implement, Google recommends that access tokens issued by the implicit flow never expire. This is because the user is forced to link their account again after a token expires with the implicit flow. If you need token expiration for security reasons, we strongly recommend that you use the authorization code flow instead.

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.

This figure shows the steps for a user to link their Google account
            to your authentication system. The first screenshot shows
            user-initiated linking from your platform. The second image shows
            user sign-in to Google, while the third shows the user consent and
            confirmation for linking their Google account with your app. The
            final screenshot shows a successfully linked user account in the
            Google app.
Figure 1. Account linking user sign in to Google and consent screens.

Requirements

  1. You must communicate that the user’s account will be linked to Google, not a specific Google product like Google Home or Google Assistant.

Recommendations

We recommend that you do the following:

  1. Display Google's Privacy Policy. Include a link to Google’s Privacy Policy on the consent screen.

  2. Data to be shared. Use clear and concise language to tell the user what data of theirs Google requires and why.

  3. 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.

  4. Ability to cancel. Provide a way for users to go back or cancel, if they choose not to link.

  5. Clear sign-in process. Ensure that users have clear method for signing in to their Google account, such as fields for their username and password or Sign in with Google.

  6. 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.

  7. 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 and the implicit flow.
  8. 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.

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 সার্ভার বাস্তবায়ন করুন

অনুমোদন কোড প্রবাহের একটি OAuth 2.0 সার্ভার বাস্তবায়নে দুটি শেষ পয়েন্ট থাকে, যা আপনার পরিষেবা HTTPS দ্বারা উপলব্ধ করে। প্রথম এন্ডপয়েন্ট হল অনুমোদনের এন্ডপয়েন্ট, যা ডেটা অ্যাক্সেসের জন্য ব্যবহারকারীদের কাছ থেকে সম্মতি খোঁজার বা পাওয়ার জন্য দায়ী। অনুমোদনের শেষ পয়েন্ট আপনার ব্যবহারকারীদের কাছে একটি সাইন-ইন UI উপস্থাপন করে যেগুলি ইতিমধ্যে সাইন ইন করেনি এবং অনুরোধ করা অ্যাক্সেসের সম্মতি রেকর্ড করে৷ দ্বিতীয় এন্ডপয়েন্ট হল টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট, যা এনক্রিপ্ট করা স্ট্রিং পেতে ব্যবহৃত হয়, যাকে টোকেন বলা হয়, যা একজন ব্যবহারকারীকে আপনার পরিষেবা অ্যাক্সেস করার অনুমতি দেয়।

যখন একটি Google অ্যাপ্লিকেশনকে আপনার পরিষেবার APIগুলির মধ্যে একটিতে কল করার প্রয়োজন হয়, তখন Google তাদের পক্ষে এই APIগুলিকে কল করার জন্য আপনার ব্যবহারকারীদের কাছ থেকে অনুমতি পেতে এই শেষ পয়েন্টগুলি একসাথে ব্যবহার করে৷

Google দ্বারা শুরু করা একটি OAuth 2.0 অনুমোদন কোড ফ্লো সেশনের নিম্নলিখিত প্রবাহ রয়েছে:

  1. Google ব্যবহারকারীর ব্রাউজারে আপনার অনুমোদনের শেষ পয়েন্ট খোলে। যদি কোনো অ্যাকশনের জন্য শুধুমাত্র ভয়েস ডিভাইসে প্রবাহ শুরু হয়, তাহলে Google এক্সিকিউশনটিকে একটি ফোনে স্থানান্তর করে।
  2. ব্যবহারকারী সাইন ইন করে, যদি ইতিমধ্যে সাইন ইন না করে থাকে, এবং যদি তারা ইতিমধ্যে অনুমতি না দেয় তবে আপনার API দিয়ে তাদের ডেটা অ্যাক্সেস করার জন্য Google-কে অনুমতি দেয়৷
  3. আপনার পরিষেবা একটি অনুমোদন কোড তৈরি করে এবং এটি Google-এ ফেরত দেয়। এটি করতে, অনুরোধের সাথে সংযুক্ত অনুমোদন কোড সহ ব্যবহারকারীর ব্রাউজারটিকে Google-এ ফেরত পাঠান৷
  4. Google আপনার টোকেন এক্সচেঞ্জ এন্ডপয়েন্টে অনুমোদন কোড পাঠায়, যা কোডের সত্যতা যাচাই করে এবং একটি অ্যাক্সেস টোকেন এবং একটি রিফ্রেশ টোকেন ফেরত দেয়। অ্যাক্সেস টোকেন হল একটি স্বল্পস্থায়ী টোকেন যা আপনার পরিষেবা API অ্যাক্সেস করার জন্য শংসাপত্র হিসাবে গ্রহণ করে। রিফ্রেশ টোকেন হল একটি দীর্ঘস্থায়ী টোকেন যা Google সংরক্ষণ করতে পারে এবং মেয়াদ শেষ হয়ে গেলে নতুন অ্যাক্সেস টোকেনগুলি অর্জন করতে ব্যবহার করতে পারে৷
  5. ব্যবহারকারী অ্যাকাউন্ট লিঙ্কিং ফ্লো সম্পূর্ণ করার পরে, Google থেকে পাঠানো প্রতিটি পরবর্তী অনুরোধে একটি অ্যাক্সেস টোকেন থাকে।

অনুমোদনের অনুরোধগুলি পরিচালনা করুন

যখন আপনাকে OAuth 2.0 অনুমোদন কোড ফ্লো ব্যবহার করে অ্যাকাউন্ট লিঙ্ক করার প্রয়োজন হয়, তখন Google নিম্নলিখিত প্যারামিটারগুলি অন্তর্ভুক্ত করে এমন একটি অনুরোধ সহ ব্যবহারকারীকে আপনার অনুমোদনের শেষ পয়েন্টে পাঠায়:

অনুমোদনের শেষ পয়েন্ট পরামিতি
client_id আপনি Google-এ যে ক্লায়েন্ট আইডি অ্যাসাইন করেছেন।
redirect_uri আপনি যে URLটিতে এই অনুরোধের প্রতিক্রিয়া পাঠান।
state পুনঃনির্দেশ URI-তে অপরিবর্তিত Google-এ ফেরত পাঠানো হয়।
scope ঐচ্ছিক: স্কোপ স্ট্রিংগুলির একটি স্পেস-ডিলিমিটেড সেট যা Google যে ডেটার জন্য অনুমোদনের অনুরোধ করছে তা নির্দিষ্ট করে৷
response_type প্রতিক্রিয়াতে যে মানের প্রত্যাবর্তন করতে হবে। OAuth 2.0 অনুমোদন কোড প্রবাহের জন্য, প্রতিক্রিয়ার ধরনটি সর্বদাই code
user_locale RFC5646 ফর্ম্যাটে Google অ্যাকাউন্টের ভাষা সেটিং, ব্যবহারকারীর পছন্দের ভাষায় আপনার বিষয়বস্তু স্থানীয়করণ করতে ব্যবহৃত হয়।

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

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

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

  1. যাচাই করুন যে client_id আপনার Google-এ নির্ধারিত ক্লায়েন্ট আইডির সাথে মেলে এবং আপনার পরিষেবার জন্য Google দ্বারা প্রদত্ত পুনঃনির্দেশ URL এর সাথে redirect_uri মেলে। অনাকাঙ্ক্ষিত বা ভুল কনফিগার করা ক্লায়েন্ট অ্যাপগুলিতে অ্যাক্সেস মঞ্জুর করা প্রতিরোধ করার জন্য এই চেকগুলি গুরুত্বপূর্ণ৷ আপনি যদি একাধিক OAuth 2.0 ফ্লো সমর্থন করেন, তাও নিশ্চিত করুন যে response_type code
  2. ব্যবহারকারী আপনার পরিষেবাতে সাইন ইন করেছেন কিনা তা পরীক্ষা করুন৷ ব্যবহারকারী সাইন ইন না করলে, আপনার পরিষেবার সাইন-ইন বা সাইন-আপ প্রবাহ সম্পূর্ণ করুন৷
  3. আপনার API অ্যাক্সেস করার জন্য Google ব্যবহার করার জন্য একটি অনুমোদন কোড তৈরি করুন। অনুমোদন কোড যেকোন স্ট্রিং মান হতে পারে, তবে এটি অবশ্যই ব্যবহারকারীকে অনন্যভাবে উপস্থাপন করতে হবে, টোকেনটি যে ক্লায়েন্টের জন্য এবং কোডের মেয়াদ শেষ হওয়ার সময়, এবং এটি অনুমানযোগ্য হতে হবে না। আপনি সাধারণত অনুমোদন কোড ইস্যু করেন যা প্রায় 10 মিনিটের পরে মেয়াদ শেষ হয়ে যায়।
  4. নিশ্চিত করুন যে redirect_uri প্যারামিটার দ্বারা নির্দিষ্ট করা URL-এর নিম্নলিখিত ফর্ম রয়েছে:
      https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID
      https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
      
  5. redirect_uri প্যারামিটার দ্বারা নির্দিষ্ট URL-এ ব্যবহারকারীর ব্রাউজারকে পুনঃনির্দেশ করুন। code এবং state প্যারামিটার যোগ করে রিডাইরেক্ট করার সময় আপনি এইমাত্র তৈরি করা অনুমোদন কোড এবং আসল, অপরিবর্তিত রাষ্ট্রীয় মান অন্তর্ভুক্ত করুন। নিম্নলিখিত ফলাফল URL এর একটি উদাহরণ:
    https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID?code=AUTHORIZATION_CODE&state=STATE_STRING

টোকেন বিনিময় অনুরোধ হ্যান্ডেল

আপনার পরিষেবার টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট দুটি ধরণের টোকেন এক্সচেঞ্জের জন্য দায়ী:

  • অ্যাক্সেস টোকেন এবং রিফ্রেশ টোকেনগুলির জন্য অনুমোদনের কোডগুলি বিনিময় করুন৷
  • অ্যাক্সেস টোকেনগুলির জন্য রিফ্রেশ টোকেনগুলি বিনিময় করুন৷

টোকেন বিনিময় অনুরোধ নিম্নলিখিত পরামিতি অন্তর্ভুক্ত:

টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট প্যারামিটার
client_id একটি স্ট্রিং যা অনুরোধের উত্সটিকে Google হিসাবে চিহ্নিত করে৷ এই স্ট্রিংটি অবশ্যই আপনার সিস্টেমের মধ্যে Google এর অনন্য শনাক্তকারী হিসাবে নিবন্ধিত হতে হবে৷
client_secret একটি গোপন স্ট্রিং যা আপনি আপনার পরিষেবার জন্য Google এর সাথে নিবন্ধন করেছেন৷
grant_type টোকেনের ধরন বিনিময় হচ্ছে। এটি হয় authorization_code বা refresh_token
code যখন grant_type=authorization_code , এই প্যারামিটারটি হল Google আপনার সাইন-ইন বা টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট থেকে প্রাপ্ত কোড।
redirect_uri grant_type=authorization_code হলে, এই প্যারামিটারটি প্রাথমিক অনুমোদনের অনুরোধে ব্যবহৃত URL।
refresh_token যখন grant_type=refresh_token , এই প্যারামিটারটি হল রিফ্রেশ টোকেন Google আপনার টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট থেকে প্রাপ্ত।
অ্যাক্সেস টোকেন এবং রিফ্রেশ টোকেনগুলির জন্য অনুমোদনের কোডগুলি বিনিময় করুন৷

ব্যবহারকারী সাইন ইন করার পরে এবং আপনার অনুমোদনের এন্ডপয়েন্ট Google-কে একটি স্বল্পকালীন অনুমোদনের কোড ফেরত দেওয়ার পরে, Google আপনার টোকেন এক্সচেঞ্জ এন্ডপয়েন্টে একটি অ্যাক্সেস টোকেন এবং একটি রিফ্রেশ টোকেনের জন্য অনুমোদন কোড বিনিময় করার জন্য একটি অনুরোধ পাঠায়।

এই অনুরোধগুলির জন্য, grant_type এর মান হল authorization_code , এবং code মান হল অনুমোদন কোডের মান যা আপনি আগে Google-কে দিয়েছিলেন৷ একটি অ্যাক্সেস টোকেন এবং একটি রিফ্রেশ টোকেনের জন্য একটি অনুমোদন কোড বিনিময় করার অনুরোধের একটি উদাহরণ নিম্নলিখিত:

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

একটি অ্যাক্সেস টোকেন এবং একটি রিফ্রেশ টোকেনের জন্য অনুমোদনের কোডগুলি বিনিময় করতে, আপনার টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট নিম্নলিখিত পদক্ষেপগুলি সম্পাদন করে POST অনুরোধে সাড়া দেয়:

  1. যাচাই করুন যে client_id অনুরোধের উত্সটিকে একটি অনুমোদিত উত্স হিসাবে চিহ্নিত করে এবং client_secret প্রত্যাশিত মানের সাথে মেলে৷
  2. যাচাই করুন যে অনুমোদন কোডটি বৈধ এবং মেয়াদ শেষ হয়নি এবং অনুরোধে উল্লেখিত ক্লায়েন্ট আইডি অনুমোদন কোডের সাথে যুক্ত ক্লায়েন্ট আইডির সাথে মেলে।
  3. নিশ্চিত করুন যে redirect_uri প্যারামিটার দ্বারা নির্দিষ্ট করা URL প্রাথমিক অনুমোদনের অনুরোধে ব্যবহৃত মানের সাথে অভিন্ন।
  4. আপনি যদি উপরের সমস্ত মানদণ্ড যাচাই করতে না পারেন, তাহলে একটি HTTP 400 খারাপ অনুরোধের ত্রুটি {"error": "invalid_grant"} দিয়ে বডি হিসাবে ফেরত দিন।
  5. অন্যথায়, একটি রিফ্রেশ টোকেন এবং একটি অ্যাক্সেস টোকেন তৈরি করতে অনুমোদন কোড থেকে ব্যবহারকারী আইডি ব্যবহার করুন৷ এই টোকেনগুলি যে কোনও স্ট্রিং মান হতে পারে, তবে সেগুলি অবশ্যই ব্যবহারকারী এবং ক্লায়েন্টকে অনন্যভাবে উপস্থাপন করতে হবে যার জন্য টোকেনটি রয়েছে এবং সেগুলি অনুমানযোগ্য হতে হবে না। অ্যাক্সেস টোকেনগুলির জন্য, টোকেনের মেয়াদ শেষ হওয়ার সময়ও রেকর্ড করুন, যা সাধারণত আপনি টোকেন ইস্যু করার এক ঘন্টা পরে। রিফ্রেশ টোকেন মেয়াদ শেষ হয় না.
  6. HTTPS প্রতিক্রিয়ার মূল অংশে নিম্নলিখিত JSON অবজেক্টটি ফেরত দিন:
    {
    "token_type": "Bearer",
    "access_token": "ACCESS_TOKEN",
    "refresh_token": "REFRESH_TOKEN",
    "expires_in": SECONDS_TO_EXPIRATION
    }

Google ব্যবহারকারীর জন্য অ্যাক্সেস টোকেন এবং রিফ্রেশ টোকেন সংরক্ষণ করে এবং অ্যাক্সেস টোকেনের মেয়াদ শেষ হওয়ার রেকর্ড করে। অ্যাক্সেস টোকেনের মেয়াদ শেষ হয়ে গেলে, Google আপনার টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট থেকে একটি নতুন অ্যাক্সেস টোকেন পেতে রিফ্রেশ টোকেন ব্যবহার করে।

অ্যাক্সেস টোকেনগুলির জন্য রিফ্রেশ টোকেনগুলি বিনিময় করুন৷

যখন একটি অ্যাক্সেস টোকেনের মেয়াদ শেষ হয়ে যায়, তখন Google একটি নতুন অ্যাক্সেস টোকেনের জন্য একটি রিফ্রেশ টোকেন বিনিময় করার জন্য আপনার টোকেন এক্সচেঞ্জ এন্ডপয়েন্টে একটি অনুরোধ পাঠায়।

এই অনুরোধগুলির জন্য, grant_type এর মান হল refresh_token , এবং refresh_token এর মান হল রিফ্রেশ টোকেনের মান যা আপনি আগে Google-কে দিয়েছিলেন৷ নিম্নলিখিতটি একটি অ্যাক্সেস টোকেনের জন্য একটি রিফ্রেশ টোকেন বিনিময় করার অনুরোধের একটি উদাহরণ:

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

একটি অ্যাক্সেস টোকেনের জন্য একটি রিফ্রেশ টোকেন বিনিময় করতে, আপনার টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট নিম্নলিখিত পদক্ষেপগুলি সম্পাদন করে POST অনুরোধে সাড়া দেয়:

  1. যাচাই করুন যে client_id অনুরোধের উৎসটিকে Google হিসাবে চিহ্নিত করে এবং client_secret প্রত্যাশিত মানের সাথে মেলে।
  2. রিফ্রেশ টোকেন বৈধ কিনা যাচাই করুন এবং অনুরোধে উল্লেখিত ক্লায়েন্ট আইডি রিফ্রেশ টোকেনের সাথে যুক্ত ক্লায়েন্ট আইডির সাথে মেলে।
  3. আপনি যদি উপরের সমস্ত মানদণ্ড যাচাই করতে না পারেন, তাহলে একটি HTTP 400 খারাপ অনুরোধের ত্রুটি {"error": "invalid_grant"} দিয়ে বডি হিসাবে ফেরত দিন।
  4. অন্যথায়, একটি অ্যাক্সেস টোকেন তৈরি করতে রিফ্রেশ টোকেন থেকে ব্যবহারকারী আইডি ব্যবহার করুন। এই টোকেনগুলি যে কোনও স্ট্রিং মান হতে পারে, তবে সেগুলি অবশ্যই ব্যবহারকারী এবং ক্লায়েন্টকে অনন্যভাবে উপস্থাপন করতে হবে যার জন্য টোকেনটি রয়েছে এবং সেগুলি অনুমানযোগ্য হতে হবে না। অ্যাক্সেস টোকেনগুলির জন্য, টোকেনের মেয়াদ শেষ হওয়ার সময়ও রেকর্ড করুন, সাধারণত আপনি টোকেন ইস্যু করার এক ঘন্টা পরে।
  5. HTTPS প্রতিক্রিয়ার মূল অংশে নিম্নলিখিত JSON অবজেক্টটি ফেরত দিন:
    {
    "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:

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:

  1. Extract access token from the Authorization header and return information for the user associated with the access token.
  2. If the access token is invalid, return an HTTP 401 Unauthorized error with using the WWW-Authenticate Response Header. Below is an example of a userinfo error response:
    HTTP/1.1 401 Unauthorized
    WWW-Authenticate: error="invalid_token",
    error_description="The Access Token expired"
    
    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.
  3. If the access token is valid, return and HTTP 200 response with the following JSON object in the body of the HTTPS response:

    {
    "sub": "USER_UUID",
    "email": "EMAIL_ADDRESS",
    "given_name": "FIRST_NAME",
    "family_name": "LAST_NAME",
    "name": "FULL_NAME",
    "picture": "PROFILE_PICTURE",
    }
    If your userinfo endpoint returns an HTTP 200 success response, the retrieved token and claims are registered against the user's Google account.

    userinfo endpoint response
    sub A unique ID that identifies the user in your system.
    email Email address of the user.
    given_name Optional: First name of the user.
    family_name Optional: Last name of the user.
    name Optional: Full name of the user.
    picture Optional: Profile picture of the user.

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

您可以使用 OAuth 2.0 Playground 工具验证您的实现。

在该工具中,执行以下步骤:

  1. 点击配置 以打开 OAuth 2.0 配置窗口。
  2. OAuth flow 字段中,选择 Client-side(客户端)。
  3. OAuth 端点字段中,选择自定义
  4. 在相应字段中指定您的 OAuth 2.0 端点和您分配给 Google 的客户端 ID。
  5. 第 1 步部分,不要选择任何 Google 范围。请将此字段留空或输入对服务器有效的范围(如果您不使用 OAuth 范围,则可以输入任意字符串)。完成后,点击授权 API
  6. Step 2Step 3 部分中,完成 OAuth 2.0 流程,并验证每个步骤是否按预期运行。

您可以使用 Google 账号关联演示版工具验证您的实现。

在该工具中,执行以下步骤:

  1. 点击使用 Google 账号登录按钮。
  2. 选择您要关联的账号。
  3. 输入服务 ID。
  4. (可选)输入您要请求访问权限的一个或多个范围。
  5. 点击开始演示
  6. 当系统提示时,请确认您同意或拒绝关联请求。
  7. 确认您已被重定向到您的平台。