从 GoogleApiClient 获取 "me" 的姓名、性别、位置
Getting name, gender, location of "me" from GoogleApiClient
在 Android 应用程序中,我已经按照示例 Accessing Google APIs 进行操作,目前 GoogleApiClient
连接成功并调用了 onConnected
回调。
但是 Google 的示例到此为止,我不知道如何检索 name、gender和我自己的 location(在 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:
中的密钥都放入了
我不明白where/if如何使用上面的"Client IDs"显示?
我只启用了 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),我的应用尚未发布。
你可以这样试试吗?如果有帮助
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);
}
}
我已经通过查看
解决了我的问题
adb shell setprop log.tag.GooglePlusPlatform VERBOSE
adb logcat
输出并启用 Google Play Android Developer API in Google Developers Console / Credentials(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);
}
}
在 Android 应用程序中,我已经按照示例 Accessing Google APIs 进行操作,目前 GoogleApiClient
连接成功并调用了 onConnected
回调。
但是 Google 的示例到此为止,我不知道如何检索 name、gender和我自己的 location(在 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:
中的密钥都放入了我不明白where/if如何使用上面的"Client IDs"显示?
我只启用了 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),我的应用尚未发布。
你可以这样试试吗?如果有帮助
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);
}
}
我已经通过查看
解决了我的问题adb shell setprop log.tag.GooglePlusPlatform VERBOSE
adb logcat
输出并启用 Google Play Android Developer API in Google Developers Console / Credentials(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);
}
}