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);}
从 GoogleAuthUtil ID 令牌流程迁移
如果您使用 GoogleAuthUtil 获取 ID 令牌,则应迁移到新的 Sign-In API ID 令牌流程。
例如,如果您的 Android 代码如下例所示,您应进行迁移:
在此示例中,ID 令牌请求使用 audience:server:client_id 以及您的网站服务器的客户端 ID 作为 GoogleAuthUtil.getToken 调用 (audience:server:client_id:9414861317621.apps.googleusercontent.com) 的“scope”(范围)参数。
[null,null,["最后更新时间 (UTC):2024-11-09。"],[[["\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)."]]