GoogleSignInResult returns DEVELOPER_ERROR in Android app when requesting server auth code

浪子不回头ぞ 提交于 2019-11-30 05:39:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!