在Android应用中,我按照示例Accessing Google APIs说明了GoogleApiClient
连接成功并且调用了onConnected
回调。
然而Google的示例就此停止,我不知道如何检索自己的名称,性别和位置(也称为正如我之前用过的JavaScript + PHP SDK for Google+中的 me 一样。)
这是Java代码(为简洁而跳过了重试和轮换处理,似乎有效)为什么me
为空?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_PROFILE)
.build();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) { // why is me null?
Person me = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
Person.Name name = me.getName();
String given = name.getGivenName();
String family = name.getFamilyName();
Log.d(TAG, "Given: " + given + ", Family: " + family);
}
请告知如何从Google +
获取登录用户的数据更新
我试图解决我的问题:
1)我已将.addScope(Plus.SCOPE_PLUS_LOGIN)
添加到构建器调用中。
2)我已经为AndroidManifest.xml添加了2个权限:
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
在Google Developers Console / Credentials我已经从我的真实密钥库和.android \ debug.keystore中放了两个密钥:
我不明白在何处/何时使用上面显示的“客户ID”?
我只启用了2个API:Google +和GCM - 可能更需要?
3)最后,我打电话给
adb shell setprop log.tag.GooglePlusPlatform VERBOSE
并且看到以下错误:
E/Volley ( 3890): [594] BasicNetwork.performRequest: Unexpected response code 403 for https://www.googleapis.com/plus/v1/people/me
D/GooglePlusPlatform( 3890): Unexpected response code (403) when requesting: getPerson
W/GooglePlusPlatform( 3890): {"errors":[{"domain":"usageLimits","reason":"accessNotConfigured","message":"Access Not Configured. The API (Google+ API) is not enabled for your project. Please use the Google Developers Console to update your configuration."}],"code":403}
我正在使用真实设备(HTC M9),我的应用尚未发布。
答案 0 :(得分:0)
public void onConnected(Bundle bundle) {
Person me = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
if (me != null) {
String personName = me.getDisplayName();
try {
int genderInt = me.getGender();//0 for male, and 1 for female
String gender = String.valueOf(genderInt);
String birthday = me.getBirthday();
System.out.println("gender" + gender + "bod" + birthday);
} catch (Exception e) {
e.printStackTrace();
}
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
}
}
答案 1 :(得分:0)
我通过查看
解决了我的问题adb shell setprop log.tag.GooglePlusPlatform VERBOSE
adb logcat
在Google Developers Console / Credentials中输出并启用 Google Play Android Developer API (以前缺少API):
以下是代码:
@Override
public void onConnected(Bundle bundle) {
Person me = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
if (me != null) {
Person.Name name = me.getName();
String given = name.getGivenName();
String family = name.getFamilyName();
boolean female = (me.hasGender() && me.getGender() == 1);
String photo = null;
if (me.hasImage() && me.getImage().hasUrl()) {
photo = me.getImage().getUrl();
}
String city = "Unknown city";
List<Person.PlacesLived> places = me.getPlacesLived();
if (places != null) {
for (Person.PlacesLived place : places) {
city = place.getValue();
if (place.isPrimary())
break;
}
}
Log.d(TAG, "Given: " + given +
", Family: " + family +
", Female: " + female +
", City: " + city);
}
}