我按照本手册将Google People API连接到Android应用:http://blog.iamsuleiman.com/people-api-android-tutorial-1/
我使用以下代码登录:
GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(new Scope(Scopes.PLUS_LOGIN),
new Scope(PeopleScopes.CONTACTS_READONLY),
new Scope(PeopleScopes.USER_PHONENUMBERS_READ))
.requestServerAuthCode(getString(R.string.google_oauth_client_id), false)
.build();
mGoogleApiClient = new GoogleApiClient.Builder(getContext())
.enableAutoManage(getActivity(), this)
.addApi(Auth.GOOGLE_SIGN_IN_API, signInOptions)
.build();
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent( mGoogleApiClient );
startActivityForResult( signInIntent, GOOGLE_PLUS_RC_SIGN_IN );
onActivityResult代码是:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
...
}
我不断得到DEVELOPER_ERROR。
该应用程序由我在开发人员控制台中设置的SHA1指纹代码签名。
OAUTH客户端ID取自我的应用程序的“Web客户端”JSON配置。
所有API均在Google开发人员控制台中启用
如果我删除方法.requestServerAuthCode():
GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(new Scope(Scopes.PLUS_LOGIN),
new Scope(PeopleScopes.CONTACTS_READONLY),
new Scope(PeopleScopes.USER_PHONENUMBERS_READ))
.build();
的结果
Auth.GoogleSignInApi.getSignInResultFromIntent(数据);
成功:该应用程序要求获得许可。
我做错了什么?
尽管在Google手册中有使用此方法的示例,为什么requestServerAuthCode会导致DEVELOPER_ERROR:
有没有示例如何在Android应用中使用People API?
而不是将您的生产/调试client_id传递给
.requestServerAuthCode()
方法使用Web client_id代替 - 我认为google signin将始终使用string.xml中的server_client_id。
所以从技术上讲,你需要使用两个键。
谷歌登录: -
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
在您的btn点击调用此方法: -
private void signIn() {
progressDialog(this, "Please wait...", true);
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
onActivityResult: -
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
progressDialog(this, "Please wait...", false);
if (requestCode == RC_SIGN_IN) {
try {
// Google Sign In was successful, authenticate with Firebase
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
} catch (Exception e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
// ...
}
}
}
最后保存您的详细信息: -
private void handleSignInResult(Task<GoogleSignInAccount> task) {
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
socialId = account.getId();
socialName = account.getDisplayName();
socialEmail = account.getEmail();
//you have got all the details now you can go your next screen
} catch (ApiException e) {
Toast.makeText(this, "Authentication failed", Toast.LENGTH_SHORT).show();
}
mGoogleSignInClient.signOut();
}