我已将我的Android应用程序签名为google控制台上的api,以及我的后端服务器作为Google控制台中的oauth客户端。
我还更新了更新的gradle文件,因此需要获取令牌ID
但不知何故,我总是无法从谷歌获得令牌
我已经从谷歌控制台检查了我的包名和ssh1代码,并通过命令与我的调试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,我在google开发者控制台中创建了web_appa2。
我从未在我的Android身份验证代码中使用我的android api_key
。我应该在哪里使用它?
我做错了哪一部分?
答案 0 :(得分:4)
这是我的清单,它可能对你有用
如果所有其他方法都失败了,我建议你尝试根据Google Developer Console中的新项目创建一个新的api密钥,祝你好运!
有用信息: developers.google.com/identity/sign-in/android/start-integrating