Oauth 2.0 如何在不请求scope userinfo.profile的情况下访问用户电子邮件信息

Oauth 2.0 如何在不请求scope userinfo.profile的情况下访问用户电子邮件信息,oauth-2.0,google-api,google-oauth,google-api-java-client,Oauth 2.0,Google Api,Google Oauth,Google Api Java Client,目前,我只想根据用户的凭据了解他们的电子邮件 public static Userinfoplus getUserInfo(Credential credentials) { Oauth2 userInfoService = new Oauth2.Builder(httpTransport, JSON_FACTORY, credentials).setApplicationName("JStock").build(); Userinfoplus userInfo

目前,我只想根据用户的凭据了解他们的电子邮件

public static Userinfoplus getUserInfo(Credential credentials)
{
    Oauth2 userInfoService =
        new Oauth2.Builder(httpTransport, JSON_FACTORY, credentials).setApplicationName("JStock").build();
    Userinfoplus userInfo = null;
    try {
        userInfo = userInfoService.userinfo().get().execute();
    } catch (IOException e) {
        System.err.println("An error occurred: " + e);
    }
    if (userInfo != null && userInfo.getId() != null) {
        return userInfo;
    } else {
        return null;
    }
}

System.out.println(getUserInfo(credential).getEmail());
但是,要使上述代码正常工作,我需要包括
https://www.googleapis.com/auth/userinfo.profile
也是

Set<String> scopes = new HashSet<String>();
scopes.add("https://www.googleapis.com/auth/userinfo.email");
scopes.add("https://www.googleapis.com/auth/userinfo.profile");
scopes.add(DriveScopes.DRIVE_APPDATA);
Set scopes=newhashset();
作用域。添加(“https://www.googleapis.com/auth/userinfo.email");
作用域。添加(“https://www.googleapis.com/auth/userinfo.profile");
添加(DriveScopes.DRIVE\u APPDATA);

我对查看有关您帐户()的基本信息不感兴趣。但是,如果我被删除
userinfo.profile
getUserInfo
将始终返回null


有没有办法进一步缩小范围?

看起来您使用的是弃用的
范围
。(见此处:)

您可能想尝试更新的。您还可以在谷歌的OAuth2游乐场上测试这一点:

它似乎只适用于
电子邮件
作为
范围
,但似乎还包括
配置文件
(顺便说一句,这很常见)