Search code examples
androidgoogle-apiaccess-tokengoogle-signin

Cannot set scope Google sign in on Android


I use Google API for signin on Android app.

Code here:

SignInButton btnLogin = (SignInButton) findViewById(R.id.sign_in_button);

btnLogin.setOnClickListener(this);

Scope[] scopes = new Scope[2];
scopes[0] = new Scope("https://www.googleapis.com/auth/admin.directory.user.readonly");
scopes[1] = new Scope("https://www.googleapis.com/auth/contacts.readonly");

btnLogin.setScopes(scopes);

Sign in success with access token, but the access token only default scopes:

"userinfo.profile" & "userinfo.email" & "auth/plus.me"

Can you help me to get 2 scopes:

"/auth/admin.directory.user.readonly" & "/auth/contacts.readonly"

Solution

  • You can refer to the following syntax:

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestScopes(new Scope(Scopes.PLUS_LOGIN)) // "https://www.googleapis.com/auth/plus.login"
                    .requestScopes(new Scope(Scopes.PLUS_ME)) // "https://www.googleapis.com/auth/plus.me"
                    .requestEmail()
                    .build();
    

    More details at this documentation. Hope it helps!