使用googleapicclient . builder的电子邮件的范围常数



Google+授权范围列表如下:https://developers.google.com/+/api/oauth。好…

在Android客户端的快速入门示例中,获取GoogleApiClient实例的代码如下所示,但带有"Plus"。SCOPE_PLUS_LOGIN"范围。但是我想用"email"范围代替。我找不到"email"作用域的常数。

  private GoogleApiClient buildGoogleApiClient() {
// When we build the GoogleApiClient we specify where connected and
// connection failed callbacks should be returned, which Google APIs our
// app uses and which OAuth 2.0 scopes our app requests.
return new GoogleApiClient.Builder(this)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .addApi(Plus.API, Plus.PlusOptions.builder().build())
    .addScope(Plus.SCOPE_PLUS_LOGIN) // I WANT AN email SCOPE!!!
    .build();

}

我必须自己创建Scope实例吗?例如:

Scope emailScope = new Scope("email"); // like this?????

您可以使用Plus.AccountApi.getAccountName()来检索登录用户的电子邮件地址:

// Note mGoogleApiClient must be connected for this to work
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

根据开发人员文档有一个注意事项:

获取Google Play服务解析的帐户名称。使用此方法必须在您的AndroidManifest.xml中声明权限<uses-permission android:name="android.permission.GET_ACCOUNTS" />

您可以为它创建一个新的作用域。以下是对我有用的:

     Builder builder = new GoogleApiClient.Builder(this)
    .addApi(Plus.API)
    .addScope(Plus.SCOPE_PLUS_PROFILE)
    .addScope(new Scope("https://www.googleapis.com/auth/userinfo.email"))

最新更新