با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
اگر در گذشته با استفاده از GoogleAuthUtil.getToken یا Plus.API با Google Sign-In ادغام شده اید، برای امنیت بیشتر و تجربه کاربری بهتر باید به جدیدترین Sign-In API مهاجرت کنید.
از ضد الگوی توکن دسترسی مهاجرت کنید
شما نباید نشانههای دسترسی بهدستآمده با GoogleAuthUtil.getToken را به عنوان ادعای هویت به سرور پشتیبان خود بفرستید، زیرا نمیتوانید به راحتی تأیید کنید که این توکن برای باطن شما صادر شده است، و شما را در مقابل درج نشانه دسترسی از سوی مهاجم آسیبپذیر میکند.
به عنوان مثال، اگر کد اندروید شما مانند مثال زیر است، باید برنامه خود را به بهترین روش های فعلی منتقل کنید.
در مثال، درخواستهای نشانه دسترسی از oauth2: بهعلاوه یک رشته دامنه بهعنوان پارامتر scope برای فراخوانی GoogleAuthUtil.getToken استفاده میکنند ( oauth2:https://www.googleapis.com/auth/plus.login ).
به جای احراز هویت با یک نشانه دسترسی که با GoogleAuthUtil.getToken به دست آمده است، از جریان رمز شناسه یا جریان کد تأیید استفاده کنید.
به جریان رمز شناسه مهاجرت کنید
اگر تنها چیزی که نیاز دارید شناسه کاربر، آدرس ایمیل، نام یا نشانی اینترنتی تصویر نمایه کاربر است، از جریان رمز شناسه استفاده کنید.
برای انتقال به جریان توکن ID، تغییرات زیر را اعمال کنید:
سمت کلاینت اندروید
در صورت درخواست، مجوز GET_ACCOUNTS (مخاطبین) را حذف کنید
هر کدی را با استفاده از GoogleAuthUtil ، Plus.API ، AccountPicker.newChooseAccountIntent() یا AccountManager.newChooseAccountIntent() به Auth.GOOGLE_SIGN_IN_API با GoogleSignInOptions.Builder.requestIdToken(...) تغییر دهید.
پس از انتقال برنامه های مشتری، نقطه پایانی قدیمی را خاموش کنید
به جریان کد تأیید اعتبار سرور مهاجرت کنید
اگر سرور شما نیاز به دسترسی به سایر APIهای Google، مانند Google Drive، Youtube یا Contacts دارد، از جریان کد احراز هویت سرور استفاده کنید.
برای انتقال به جریان کد احراز هویت سرور، تغییرات زیر را اعمال کنید:
سمت کلاینت اندروید
در صورت درخواست، مجوز GET_ACCOUNTS (مخاطبین) را حذف کنید
هر کدی را با استفاده از GoogleAuthUtil ، Plus.API ، AccountPicker.newChooseAccountIntent() یا AccountManager.newChooseAccountIntent() به Auth.GOOGLE_SIGN_IN_API با GoogleSignInOptions.Builder.requestServerAuthCode(...) تغییر دهید.
پس از انتقال برنامه های مشتری، نقطه پایانی قدیمی را خاموش کنید
همچنان میتوانید منطق دسترسی API را بین نقاط پایانی قدیمی و جدید خود به اشتراک بگذارید. به عنوان مثال:
GoogleTokenResponsetokenResponse=newGoogleAuthorizationCodeTokenRequest(...);StringaccessToken=tokenResponse.getAccessToken();StringrefreshToken=tokenResponse.getRefreshToken();LongexpiresInSeconds=tokenResponse.getExpiresInSeconds();// Shared by your old and new implementation, old endpoint can pass null for refreshTokenprivatevoiddriveAccess(StringrefreshToken,StringaccessToken,LongexpiresInSeconds){GoogleCredentialcredential=newGoogleCredential.Builder().setTransPort(...)....build();credential.setAccessToken(accessToken);credential.setExpiresInSeconds(expiresInSeconds);credential.setRefreshToken(refreshToken);}
از جریان توکن ID GoogleAuthUtil مهاجرت کنید
اگر از GoogleAuthUtil برای دریافت نشانه های شناسه استفاده می کنید، باید به جریان نشانه شناسه API Sign-In جدید منتقل شوید.
برای مثال، اگر کد اندروید شما مانند مثال زیر است، باید مهاجرت کنید:
در مثال، درخواستهای نشانه شناسه از audience:server:client_id بهعلاوه شناسه مشتری برای سرور وب شما بهعنوان پارامتر «scope» برای تماس GoogleAuthUtil.getToken استفاده میکنند ( audience:server:client_id:9414861317621.apps.googleusercontent.com ).
جریان رمز جدید Sign-In API ID دارای مزایای زیر است:
تجربه ورود به سیستم با یک ضربه ساده
سرور شما می تواند اطلاعات نمایه کاربر را بدون تماس شبکه اضافی دریافت کند
برای انتقال به جریان توکن ID، تغییرات زیر را اعمال کنید:
سمت کلاینت اندروید
در صورت درخواست، مجوز GET_ACCOUNTS (مخاطبین) را حذف کنید
هر کدی را با استفاده از GoogleAuthUtil ، Plus.API ، AccountPicker.newChooseAccountIntent() یا AccountManager.newChooseAccountIntent() به Auth.GOOGLE_SIGN_IN_API با GoogleSignInOptions.Builder.requestIdToken(...) تغییر دهید.
سمت سرور
Sign-In API جدید، بر خلاف GoogleAuthUtil.getToken ، که از قالب منسوخ استفاده میکند، نشانههای شناسهای صادر میکند که با مشخصات OpenID Connect مطابقت دارند. به ویژه، صادرکننده اکنون https://accounts.google.com با طرحی https است.
در طول فرآیند مهاجرت، سرور شما باید کد شناسه را از کلاینتهای قدیمی و جدید اندروید شما تأیید کند. برای تأیید هر دو فرمت نشانه، تغییری را انجام دهید که مربوط به کتابخانه مشتری مورد استفاده شما است (اگر از یکی استفاده می کنید):
جاوا (Google APIs Client Libraries): به 1.21.0 یا جدیدتر ارتقا دهید
PHP (Google APIs Client Libraries): اگر از v1 استفاده می کنید، آن را به 1.1.6 یا جدیدتر ارتقا دهید. اگر از نسخه 2 استفاده می کنید، به 2.0.0-RC1 یا جدیدتر ارتقا دهید
Node.js: به 0.9.7 یا جدیدتر ارتقا دهید
پایتون یا پیاده سازی های خودتان: هر دوی این صادرکنندگان را بپذیرید: https://accounts.google.com و accounts.google.com
از جریان کد احراز هویت سرور GoogleAuthUtil مهاجرت کنید
اگر از GoogleAuthUtil برای دریافت کد احراز هویت سرور استفاده میکنید، باید به جریان کد احراز هویت API جدید Sign-In مهاجرت کنید.
برای مثال، اگر کد اندروید شما مانند مثال زیر است، باید مهاجرت کنید:
در مثال، درخواستهای کد احراز هویت سرور oauth2:server:client_id + شناسه مشتری برای وب سرور شما بهعنوان پارامتر scope برای تماس GoogleAuthUtil.getToken ( oauth2:server:client_id:9414861317621.apps.googleusercontent.com ) استفاده میکنند.
جریان کد احراز هویت جدید Sign-In API دارای مزایای زیر است:
تجربه ورود به سیستم با یک ضربه ساده
اگر از راهنمای انتقال زیر پیروی کنید، سرور شما میتواند هنگام تبادل کد احراز هویت، یک کد شناسه حاوی اطلاعات نمایه کاربر دریافت کند.
برای انتقال به جریان کد اعتبار جدید، تغییرات زیر را اعمال کنید:
سمت کلاینت اندروید
در صورت درخواست، مجوز GET_ACCOUNTS (مخاطبین) را حذف کنید
هر کدی را با استفاده از GoogleAuthUtil ، Plus.API ، AccountPicker.newChooseAccountIntent() یا AccountManager.newChooseAccountIntent() به Auth.GOOGLE_SIGN_IN_API با GoogleSignInOptions.Builder.requestServerAuthCode(...) تغییر دهید.
سمت سرور
کد فعلی خود را نگه دارید، اما در هنگام ساختن شیء GoogleAuthorizationCodeTokenRequest ، https://oauth2.googleapis.com/token به عنوان نقطه پایانی سرور نشانه مشخص کنید، تا بتوانید یک رمز شناسه با ایمیل، شناسه کاربر و اطلاعات نمایه کاربر بدون نیاز به تماس شبکه دیگری این نقطه پایانی کاملاً به عقب سازگار است و کد زیر برای کدهای تأیید اعتبار سرور بازیابی شده از هر دو اجرای کلاینت Android قدیمی و جدید شما کار خواهد کرد.
تاریخ آخرین بهروزرسانی 2024-11-19 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2024-11-19 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eGoogle Sign-In for Android is outdated and developers should migrate to Credential Manager for enhanced security and user experience.\u003c/p\u003e\n"],["\u003cp\u003eAvoid sending access tokens directly to backend servers for authentication; instead, utilize the ID token or server auth code flow.\u003c/p\u003e\n"],["\u003cp\u003eMigrating to the ID token flow is recommended for retrieving basic user information like ID, email, and profile details.\u003c/p\u003e\n"],["\u003cp\u003eThe server auth code flow is suitable for accessing Google APIs beyond user profile data, such as Google Drive or YouTube.\u003c/p\u003e\n"],["\u003cp\u003eWhen migrating from GoogleAuthUtil, ensure your server can verify ID tokens from both old and new clients by updating client libraries or accepting both issuer formats.\u003c/p\u003e\n"]]],[],null,["# Migrate from GoogleAuthUtil and Plus.API\n\n| **Warning:** Google Sign-In for Android is outdated and no longer supported. To ensure the continued security and usability of your app, [migrate\n| to Credential Manager](https://developer.android.com/training/sign-in/credential-manager/) today. Credential Manager supports passkey, password, and federated identity authentication (such as Sign-in with Google), stronger security, and a more consistent user experience. For Wear developers: Credential Manager will be supported in Wear OS 5.1 and later on selected watches. Developers actively supporting Wear OS 3, 4 and 5.0 devices with Sign in with Google should continue using Google Sign-in for Android for your Wear applications. Sign in with Google support will be available on Credential Manager APIs for these versions of WearOS at a later date.\n\nIf you integrated with Google Sign-In in the past using\n`GoogleAuthUtil.getToken` or `Plus.API`, you should migrate to the newest\nSign-In API for greater security and a better user experience.\n\nMigrate from access token anti-pattern\n--------------------------------------\n\nYou should not send access tokens obtained with `GoogleAuthUtil.getToken` to\nyour backend server as an identity assertion, since you cannot easily verify\nthat the token was issued to your backend, leaving you vulnerable to insertion\nof an access token from an attacker.\n\nFor example, if your Android code looks like the example below, you should\nmigrate your app to the current best practices.\n\nIn the example, access token requests use `oauth2:` plus a scope string as the\n`scope` parameter for the `GoogleAuthUtil.getToken` call\n(`oauth2:https://www.googleapis.com/auth/plus.login`).\n\nInstead of authenticating with an access token acquired with\n`GoogleAuthUtil.getToken`, use either the ID token flow or the auth code flow.\n\n### Migrate to the ID token flow\n\nIf all you need is the user's ID, email address, name, or profile picture URL,\nuse the [ID token flow](/identity/sign-in/android/backend-auth).\n\nTo migrate to the ID token flow, make the following changes:\n\n#### Android client side\n\n- Remove the `GET_ACCOUNTS` (Contacts) permission if you request it\n- Switch any code using `GoogleAuthUtil`, `Plus.API`, `AccountPicker.newChooseAccountIntent()`, or `AccountManager.newChooseAccountIntent()` to `Auth.GOOGLE_SIGN_IN_API` with `GoogleSignInOptions.Builder.requestIdToken(...)` configuration.\n\n#### Server side\n\n- Create a new endpoint for [ID token auth](/identity/sign-in/android/backend-auth)\n- Turn off the old endpoint after your client apps are migrated\n\n### Migrate to the server auth code flow\n\nIf your server needs to access other Google APIs, such as Google Drive, Youtube,\nor Contacts, use the [server auth code flow](/identity/sign-in/android/offline-access).\n\nTo migrate to the server auth code flow, make the following changes:\n\n#### Android client side\n\n- Remove the `GET_ACCOUNTS` (Contacts) permission if you request it\n- Switch any code using `GoogleAuthUtil`, `Plus.API`, `AccountPicker.newChooseAccountIntent()`, or `AccountManager.newChooseAccountIntent()` to `Auth.GOOGLE_SIGN_IN_API` with `GoogleSignInOptions.Builder.requestServerAuthCode(...)` configuration.\n\n#### Server side\n\n- Create a new endpoint for the [server auth code flow](/identity/sign-in/android/offline-access)\n- Turn off the old endpoint after your client apps are migrated\n\nYou can still share API access logic between your old and new endpoints. For\nexample: \n\n```carbon\nGoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(...);\nString accessToken = tokenResponse.getAccessToken();\nString refreshToken = tokenResponse.getRefreshToken();\nLong expiresInSeconds = tokenResponse.getExpiresInSeconds();\n\n// Shared by your old and new implementation, old endpoint can pass null for refreshToken\nprivate void driveAccess(String refreshToken, String accessToken, Long expiresInSeconds) {\n GoogleCredential credential = new GoogleCredential.Builder()\n .setTransPort(...)\n ...\n .build();\n credential.setAccessToken(accessToken);\n credential.setExpiresInSeconds(expiresInSeconds);\n credential.setRefreshToken(refreshToken);\n}\n```\n\nMigrate from the GoogleAuthUtil ID token flow\n---------------------------------------------\n\nIf you use `GoogleAuthUtil` to get ID tokens, you should migrate to the new\nSign-In API [ID token flow](/identity/sign-in/android/backend-auth).\n\nFor example, if your Android code looks like the following example, you should\nmigrate:\n\nIn the example, ID token requests use `audience:server:client_id` plus the\nclient ID for your web server as the 'scope' parameter for the\n`GoogleAuthUtil.getToken` call\n(`audience:server:client_id:9414861317621.apps.googleusercontent.com`).\n\nThe new Sign-In API ID token flow has the following benefits:\n\n- Streamlined one-tap sign-in experience\n- Your server can get user profile information without an extra network call\n\nTo migrate to the ID token flow, make the following changes:\n\n#### Android client side\n\n- Remove the `GET_ACCOUNTS` (Contacts) permission if you request it\n- Switch any code using `GoogleAuthUtil`, `Plus.API`, `AccountPicker.newChooseAccountIntent()`, or `AccountManager.newChooseAccountIntent()` to `Auth.GOOGLE_SIGN_IN_API` with `GoogleSignInOptions.Builder.requestIdToken(...)` configuration.\n\n#### Server side\n\nThe new Sign-In API issues ID tokens that comply with the OpenID Connect\nspecification, unlike `GoogleAuthUtil.getToken`, which uses a deprecated format.\nIn particular, the issuer is now `https://accounts.google.com`, with a `https`\nschema.\n\nDuring your migration process, your server needs to verify ID token from both\nyour old and new Android clients. To verify both formats of the token, make the\nchange that corresponds to the client library you use (if you use one):\n\n- Java (Google APIs Client Libraries): upgrade to 1.21.0 or newer\n- PHP (Google APIs Client Libraries): if you use v1, upgrade to 1.1.6 or newer; if you use v2, upgrade to 2.0.0-RC1 or newer\n- Node.js: upgrade to 0.9.7 or newer\n- Python or your own implementations: accept both of these issuers: `https://accounts.google.com` and `accounts.google.com`\n\nMigrate from the GoogleAuthUtil server auth code flow\n-----------------------------------------------------\n\nIf you use `GoogleAuthUtil` to get a server auth code, you should migrate to the\nnew Sign-In API [auth code flow](/identity/sign-in/android/offline-access).\n\nFor example, if your Android code looks like the following example, you should\nmigrate:\n\nIn the example, server auth code requests use `oauth2:server:client_id` + the\nclient ID for your web server as the `scope` parameter for the\n`GoogleAuthUtil.getToken` call (`oauth2:server:client_id:9414861317621.apps.googleusercontent.com`).\n\nThe new Sign-In API auth code flow has the following benefits:\n\n- Streamlined one-tap sign-in experience\n- If you follow the migration guide below, your server can get an ID token containing the user's profile information when you do the auth code exchange\n\nTo migrate to the new auth code flow, make the following changes:\n\n#### Android client side\n\n- Remove the `GET_ACCOUNTS` (Contacts) permission if you request it\n- Switch any code using `GoogleAuthUtil`, `Plus.API`, `AccountPicker.newChooseAccountIntent()`, or `AccountManager.newChooseAccountIntent()` to `Auth.GOOGLE_SIGN_IN_API` with `GoogleSignInOptions.Builder.requestServerAuthCode(...)` configuration.\n\n#### Server side\n\nKeep your current code, but specify `https://oauth2.googleapis.com/token`\nas the token server endpoint when constructing the\n`GoogleAuthorizationCodeTokenRequest` object, so that you can get an ID token\nwith the user's email, user ID, and profile info without the need for another\nnetwork call. This endpoint is fully backward compatible, and the below code\nwill work for server auth codes retrieved from both your old and new Android\nclient implementations. \n\n```css+lasso\nGoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(\n transport,\n jsonFactory,\n // Use below for tokenServerEncodedUrl parameter\n \"https://oauth2.googleapis.com/token\",\n clientSecrets.getDetails().getClientId(),\n clientSecrets.getDetails().getClientSecret(),\n authCode,\n REDIRECT_URI)\n .execute();\n\n...\n\n// You can also get an ID token from auth code exchange.\nGoogleIdToken googleIdToken = tokenResponse.parseIdToken();\nGoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, jsonFactory)\n .setAudience(Arrays.asList(SERVER_CLIENT_ID))\n .setIssuer(\"https://accounts.google.com\")\n .build();\n// Refer to ID token documentation to see how to get data from idToken object.\nGoogleIdToken idToken = verifier.verify(idTokenString);\n...\n```\n| **Note:** If you use the server auth code flow, but only need to access the user's profile information on your server, consider switching to the recommended [ID token flow](/identity/sign-in/android/backend-auth)."]]