无法从Google+;获取登录用户的信息;在android中
我正在我的应用程序中使用Google+唱歌。当我点击Google按钮时,onConnect()方法被调用,但从onConnect()方法中,我正在从Google检索一些信息。但是“Plus.PeopleApi.getCurrentPerson(mGoogleApiClient)”方法返回空值。请帮帮我无法从Google+;获取登录用户的信息;在android中,android,google-plus,Android,Google Plus,我正在我的应用程序中使用Google+唱歌。当我点击Google按钮时,onConnect()方法被调用,但从onConnect()方法中,我正在从Google检索一些信息。但是“Plus.PeopleApi.getCurrentPerson(mGoogleApiClient)”方法返回空值。请帮帮我 // Google API Services CallBack Method private boolean mSignInClicked; private Con
// Google API Services CallBack Method
private boolean mSignInClicked;
private ConnectionResult mConnectionResult;
private void loginWithGoogle() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).addApi(Plus.API, null)
.addScope(Plus.SCOPE_PLUS_PROFILE)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle arg0) {
mSignInClicked = false;
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi
.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
Log.i("personName", personName);
Image personPhoto = currentPerson.getImage();
String photoUrl = personPhoto.getUrl();
Log.i("photoUrl", photoUrl);
String personGooglePlusProfile = currentPerson.getUrl();
Log.i("personGooglePlusProfile", personGooglePlusProfile);
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
Log.i("email", email);
boolean hasBirthday = currentPerson.hasBirthday();
Log.i("hasBirthday", "" + hasBirthday);
String birthday = currentPerson.getBirthday();
Log.i("birthday", "" + birthday);
boolean hasAbout = currentPerson.hasAboutMe();
Log.i("hasAbout", "" + hasAbout);
String about = currentPerson.getAboutMe();
Log.i("about", "" + about);
String Id = currentPerson.getId();
Log.i("Id", Id);
int gender = currentPerson.getGender();
Log.i("gender", "" + gender);
boolean hasLocation = currentPerson.hasCurrentLocation();
Log.i("hasLocation", "" + hasLocation);
String location = currentPerson.getCurrentLocation();
Log.i("location", "" + location);
AgeRange agerange = currentPerson.getAgeRange();
if (agerange != null)
Log.i("agerange", "" + agerange.toString());
Name name = currentPerson.getName();
Log.i("getFamilyName", name.getFamilyName());
btnGoogle = true;
new ApiCall().execute(url);
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
}
} else {
Log.i("personName", "getCurrentPerson(mGoogleApiClient) is null");
}
}
@Override
public void onConnectionSuspended(int arg0) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (!mIntentInProgress) {
mConnectionResult = result;
if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to
// resolve
// all
// errors until the user is signed in, or they cancel.
resolveSignInError();
}
}
}
public void resolveSignInError() {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
} catch (SendIntentException e) {
// The intent was canceled before it was sent. Return to the
// default
// state and attempt to connect to get an updated
// ConnectionResult.
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
您必须添加以下行:Plus.PeopleApi.loadVisible(mgoogleAppClient,null).setResultCallback(this);在未连接的
中
像这样:
@Override
public void onConnected(Bundle connectionHint) {
/* This Line is the key */
Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);
// After that fetch data
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi
.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
Log.i("personName", personName);
......
}
}
参考:
希望对你有帮助ツ 这解决了我的问题,我包括了debug-SHA1键和release-SHA1键,并且能够返回结果。根据您测试应用程序的方式,在“Google开发者控制台”中,尝试使用debug SHA1创建一个新的客户端密钥,然后重试(这样您的api控制台上将有两个客户端密钥,一个用于发布,一个用于调试)。如果您使用的是Android Studio,这很可能就是原因所在。问题在于证书 1) 您是否使用了正确的SHA1签名来生成json配置文件和正确的包名 2) 您是否使用相同的证书签署申请
2a)请记住,无论构建版本如何,Android Studio都将始终为您的应用程序签名。如果未指定,将使用默认开发。因此,除非您提供了dev证书签名来生成配置,否则很可能是不匹配的。因此为空。是否添加了
Plus.PeopleApi.loadVisible(mGoogleApiClient,null).setResultCallback(此)代码>?在哪里?在onConnect()方法中?是的,在OnConnected
中,请您提前告诉我完整的onConnect()方法谢谢您是否可以从google plus获得出生日期?否,我在onConnect()方法中添加了“plus.PeopleApi.loadVisible(mGoogleApiClient,null)。setResultCallback(this);”。我已经实现了它的接口。但是它从Mestil开始不起作用,你只得到空值吗??确保我使用了你提供的链接,并且在onResult()方法中“peopleData.getStatus().getStatusCode()”给了我7次尝试或