我已经在 google 控制台上将我的 android 应用程序作为 api 签名,并将我的后端服务器作为 google 控制台中的 oauth 客户端。
我还更新了我的 gradle 文件,因此需要什么来获取令牌 ID。
但不知何故,我总是无法从谷歌获得令牌。
我已经从谷歌控制台检查了我的包名称和 ssh1 代码,并通过 with 命令将其与我的调试 ssh1 匹配:keytool -list -v -keystore mydebudpath\debug.keystore
我将在这里提交我的 android 代码:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
validateServerClientID();
// [START configure_signin]
// Request only the user's ID token, which can be used to identify the
// user securely to your backend. This will contain the user's basic
// profile (name, profile picture URL, etc) so you should not need to
// make an additional call to personalize your application.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.server_client_id))
.requestServerAuthCode(getString(R.string.server_client_id), false)
.requestEmail()
.build();
// [END configure_signin]
// Build GoogleAPIClient with the Google Sign-In API and the above options.
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.enableAutoManage(getActivity()/* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
和我的 onActivityResult:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_GET_TOKEN) {
// [START get_id_token]
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
Log.d(TAG, "onActivityResult:GET_TOKEN:success:" + result.getStatus().isSuccess());
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
String idToken = acct.getIdToken();
// Show signed-in UI.
Log.d(TAG, "idToken:" + idToken);
updateUI(true);
// TODO(user): send token to server and validate server-side
} else {
// Show signed-out UI.
updateUI(false);
}
// [END get_id_token]
}
}
代码result.getStatus().isSuccess();
总是返回false
请注意,在上面的代码中,我R.string.server_client_id
是我的服务器的 client_id,我在谷歌开发者控制台中创建为 web 应用程序 oauth2。
我从未api_key
在我的 android 身份验证代码中使用过我的 android。我应该在哪里使用它?
我做错了哪一部分?