无法在Android上设置Google登录范围

时间:2016-01-09 06:08:24

标签: android google-api access-token google-signin

我使用Google API登录Android应用。

代码在这里:

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);

使用访问令牌登录成功,但访问令牌只有默认范围:

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

你能帮助我获得两个范围:

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

2 个答案:

答案 0 :(得分:19)

您可以参考以下语法:

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();

this documentation的更多详情。希望它有所帮助!

答案 1 :(得分:1)

您可以在requestScopes()中添加多个范围。

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestScopes(new Scope(Scopes.PLUS_LOGIN), new Scope(Scopes.PLUS_ME))
                .requestEmail()
                .build();