使用google +登录时,我需要添加什么范围来获取用户电子邮件?

问题描述:

我正在尝试从google plus api获取访问令牌.

I am trying to get a access token from the google plus api.

我运行以下代码:

private class RetrieveTokenTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        String accountName = params[0];
        Log.e("accountName", accountName);
        String scopes = "oauth2:" + Scopes.PLUS_ME; //

        String token = null;
        try {
            token = GoogleAuthUtil.getToken(getApplicationContext(), accountName, scopes);
        } catch (IOException e) {
            Log.e("GOOGLE+", e.getMessage());
        } catch (UserRecoverableAuthException e) {
            startActivityForResult(e.getIntent(), RC_SIGN_IN);
        } catch (GoogleAuthException e) {
            Log.e("GOOGLE+", e.getMessage());
        }
        return token;
    }

    @Override
    protected void onPostExecute(String s) {
        Log.d("TOKEN", "token: " + s);
    }
}

我使用HTTP GET检查邮递员中的访问令牌 https://www.googleapis.com/oauth2/v1/userinfo 带有标题Authorization Bearer

I check my access token in postman with a HTTP GET to https://www.googleapis.com/oauth2/v1/userinfo with header Authorization Bearer

结果:

{
id: "111111111111"
name: "my name"
given_name: "my"
family_name: "name"
link: https://plus.google.com/123456789
picture: https:....jpg
gender: "male"
}

如何获取用户的电子邮件?我需要使用什么范围?

How can I get the user's email? What scope do I need to use?

来自 https://developers .google.com/+/api/oauth :

不建议将https://www.googleapis.com/auth/plus.me范围用作登录范围,因为对于尚未升级到Google+的用户,它不会返回用户名或电子邮件地址.

The https://www.googleapis.com/auth/plus.me scope is not recommended as a login scope because, for users who have not upgraded to Google+, it does not return the user's name or email address.

相反,建议您使用profile范围或https://www.googleapis.com/auth/plus.login范围(可作为Scopes.PLUS_LOGIN使用).

Instead, it is suggested you either use the profile scope or the https://www.googleapis.com/auth/plus.login scope (which is available as Scopes.PLUS_LOGIN).

此外,您将需要email范围(这是范围的全名,它不是URL格式),因此电子邮件地址将作为呼叫的一部分返回.

Additionally you will need the email scope (this is the full name of the scope, it is not in URL format) so the email address will be returned as part of the call.

您的范围定义可能类似于:

Your scopes definition might look something like:

String scopes = "oauth2:email " + Scopes.PLUS_LOGIN;

String scopes = "oauth2:profile email"; 

https://www.googleapis.com/oauth2/v1/userinfo 端点已弃用,不定期工作,计划于9月删除.

The https://www.googleapis.com/oauth2/v1/userinfo endpoint has been deprecated, works sporadically, and is scheduled to be removed in September.

相反,您应该使用 https://www.googleapis.com/plus/v1 /people/me ("me"代表经过身份验证的用户的用户ID),并向Authorization标头提供与之前相同的Bearer令牌.

Instead, you should use https://www.googleapis.com/plus/v1/people/me (the "me" represents the userid for the authenticated user) and provide the Authorization header with the same Bearer token you were before.

有关弃用和迁移的详细信息,请参见 https://developers.google. com/+/api/auth-migration

For more information about the deprecation and migration, see https://developers.google.com/+/api/auth-migration