SAR*_*ose 7 java authentication android google-signin google-identity
我按照这些说明(https://developers.google.com/identity/sign-in/android/backend-auth)获取ID令牌以便发送到我的后端,但是当我设置时String scopes = "audience:server:client_id:" + Service.SERVER_CLIENT_ID;(是的,SERVER_CLIENT_ID这不是Android)客户端ID)我无法获得令牌并抛出此错误.
E/Login: com.google.android.gms.auth.GoogleAuthException: Unknown
但是当我使用以下范围时
String scopes = "oauth2:profile email";
我成功地获得了'一个'令牌,但它没有我想象的那么长,我担心这可能是错的.
我的问题是......
1)为什么scopes = "audience:server:client_id:" + SERVER_CLIENT_ID;指南中使用的不起作用?
2)我是否使用String scopes = "oauth2:profile email";安全的令牌来验证后端用户?
代码如下.
@Override
protected String doInBackground(Void... params) {
String accountName = Plus.AccountApi.getAccountName(googleApiClient);
Account account = new Account(accountName, GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
//String scopes = "oauth2:profile email";
String scopes = "audience:server:client_id:" + Service.SERVER_CLIENT_ID; // Not the app's client ID.
Log.d(TAG, "Account Name: " + accountName);
Log.d(TAG, "Scopes: " + scopes);
try {
userIdToken = GoogleAuthUtil.getToken(getApplicationContext(), account, scopes);
return userIdToken;
} catch (IOException e) {
Log.e(TAG, "IOError retrieving ID token.", e);
return null;
} catch (UserRecoverableAuthException e) {
startActivityForResult(e.getIntent(), RC_SIGN_IN);
return null;
} catch (GoogleAuthException e) {
Log.e(TAG, "GoogleAuthError retrieving ID token.", e);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
当您将范围设置为 oauth2:profile email 时,您将返回一个访问令牌,该令牌与 id 令牌不同。
访问令牌可用于访问 Google API,id 令牌是 JWT,其中包含由 Google 数字签名的用户身份信息。格式不同。如果您尝试使用为 id 令牌提供的示例代码来授权访问令牌,您将收到无效错误。
如果您查看 GoogleAuthUtil.getToken() 的文档,您会发现 GoogleAuthException 是致命异常,通常由客户端错误(例如无效范围或无效客户端)引起。 https://developers.google.com/android/reference/com/google/android/gms/auth/GoogleAuthUtil#getToken(android.content.Context,android.accounts.Account,java.lang.String,android.os。捆)
确保您已在 Google 开发者控制台中设置应用程序和网络服务器 oAuth2 ID,并且清单中的程序包名称与您在创建应用程序 ID 时提供的程序包名称以及 SHA 指纹相匹配。使用 Web 服务器 ID 作为 SERVER_CLIENT_ID。
我将一些示例代码上传到了 Github。https://github.com/kmosdev/google-signin-backend-auth
我从 Google 的示例登录应用程序开始,并对其进行了修改以添加后端身份验证。更多详细信息请参见自述文件。
另一件需要检查的事情是您的清单文件中是否具有正确的权限,但我相信如果这是错误的,您会收到不同的错误:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2048 次 |
| 最近记录: |