问题
I am connecting Google People API to the Android app following this manual: http://blog.iamsuleiman.com/people-api-android-tutorial-1/
I am using the following code to sign in:
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 );
The onActivityResult code is:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
...
}
I keep getting DEVELOPER_ERROR in result.
The app is signed by the code which SHA1 fingerprint I setup in the developer console.
The OAUTH client ID is taken from the "Web client" JSON configuration of my app.
All APIs are enabled in the Google developer console
If I remove the method .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();
The result of
Auth.GoogleSignInApi.getSignInResultFromIntent(data);
is successful: the app is asking for the permission.
What I am doing wrong?
Why requestServerAuthCode causes DEVELOPER_ERROR, despite there is an example of using this method in Google's manual:
https://developers.google.com/identity/sign-in/android/offline-access#enable_server-side_api_access_for_your_app
Is there any sample how to use People API in Android app?
回答1:
Instead of passing your production/debug client_id into the
.requestServerAuthCode()
method use a Web client_id instead - i think that google signin will always use the server_client_id from the string.xml.
So technically you need to use both keys.
回答2:
google sign in:-
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
on your btn click call this method:-
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);
// ...
}
}
}
finally save your deatils:-
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();
}
来源:https://stackoverflow.com/questions/44870165/googlesigninresult-returns-developer-error-in-android-app-when-requesting-server