我按照本手册将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(数据);
成功:应用程序要求获得许可。
我做错了什么?
为什么requestServerAuthCode导致DEVELOPER_ERROR,尽管在Google手册中有使用此方法的示例:
是否有任何示例如何在Android应用中使用People API?
答案 0 :(得分:0)
与其将生产/调试client_id传递到
.requestServerAuthCode()
方法改用Web client_id-我认为Google登录将始终使用string.xml中的 server_client_id 。
因此从技术上讲,您需要同时使用两个键。
答案 1 :(得分:0)
google登录:-
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();
}