【发布时间】:2016-04-08 08:46:57
【问题描述】:
编辑
经过新的尝试,我终于迎来了我的生日。我没有改变实现。我想我的 Google Plus 个人资料中的更改需要几个小时才能传播,从而公开我的生日。有点超出预期。现在我想知道是否可以在用户登录时询问此权限(生日),而无需公开生日。
正如developers guide 中的此信息所建议的那样:
有关可能可用的其他个人资料数据,请参阅 谷歌登录帐户。请注意,任何配置文件字段都可以为空, 取决于您要求的范围和信息 用户的个人资料包括。
初始 POST
我想在用户登录 Google Plus 后获取他的生日。由于一般不熟悉 Google API,我想知道我的实现是否有意义。现在,我只是想通过与我的设备关联的 Google 帐户来获取自己的生日。
这就是我所做的。我按照developer guide 的步骤进行了登录过程。除了 OAuth,我还在 gradle 中添加了 Google Plus 依赖:
编译'com.google.android.gms:play-services-plus:8.3.0'
在我的Activity 中,有一个用于登录 Google Plus 的按钮。
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(Scopes.PLUS_ME), new Scope(Scopes.PLUS_LOGIN),new Scope(Scopes.PROFILE))
.build();
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.build();
loginButton = (SignInButton) findViewById(R.id.sign_in_button);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
});
初次登录后,我尝试连接到 Google Play 服务。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
}
}
}
连接后,我会尝试获取用户的生日。
@Override
public void onConnected(Bundle bundle) {
Person currentPerson = Plus.PeopleApi
.getCurrentPerson(mGoogleApiClient);
addGoogleProfile(currentPerson); //Add the user to a list of persons
refreshListView(); //update the listView
Log.d(TAG, "Current person: name=" + currentPerson.getDisplayName() + ", has birthday = " + (currentPerson.hasBirthday() ? "yes, it is" + currentPerson.getBirthday() : "no"));
}
问题
生日永远不会设置并且总是显示为空。可以访问其他数据,例如姓名和个人资料图像。我没有找到关于不同Scopes 的太多信息。我更熟悉 Facebook API,在登录时更明确地请求权限。
知道我缺少什么吗?
何和我将我的生日字段设置为关联 Google 帐户的“公开”。
干杯
【问题讨论】:
-
嗨。感谢您的回答。但是,我不明白如何获取其他信息。来自developers.google.com/android/reference/com/google/android/gms/…,似乎只有几个字段可用。
-
您可能会发现 Scopes here 的可能值。 PLUS_LOGIN 和 PROFILE 值得一看。
标签: android google-plus google-play-services