使用 Google+ API 获取用户凭据
Getting user credentials using Google+ API
我正在尝试使用 Google+ [=32] 在我的 android 应用程序中包含 Google 登录 =]. 我可以从用户那里获取帐户详细信息,但是一旦登录,当 请求用户名 时,我得到 null使用电话:
Plus.PeopleApi.getCurrentPerson(mGoogleApiClient).getDisplayName()
并且Logcat显示:
BasicNetwork.performRequest: Unexpected response code 403 for
https://www.googleapis.com/plus/v1/people/me
虽然我可以使用以下方式获取用户的电子邮件:
Plus.AccountApi.getAccountName(GoogleClient.mGoogleApiClient)
请帮助我发现我的错误
我认为 google+ 应用程序中登录集成的基本步骤已为 you.Here 所知,其余步骤为
第一步--
在 oncreate
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
第2步--
在登录按钮中单击侦听器调用此----
private void LoginGoogle(){
int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
if (errorCode != ConnectionResult.SUCCESS) {
GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0).show();
}
else{
//perform login
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
mGoogleApiClient.connect();
}
}
}
第3步----
在 onActivityresult
if (requestCode == RC_SIGN_IN) {
if (resultCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.reconnect();
}
}
step 4-----
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
if(!mIntentInProgress){
if ( mSignInClicked && result.hasResolution()) {
// The user has already clicked 'sign-in' so we attempt to resolve all
// errors until the user is signed in, or they cancel.
try {
result.startResolutionForResult(this, RC_SIGN_IN);
mIntentInProgress = true;
} 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();
}
}
}
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
mSignInClicked = false;
getProfileInformation();
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
步骤 5-----
private void getProfileInformation(){
try {
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String id=currentPerson.getId();
String personName = currentPerson.getDisplayName();
String personPhoto = currentPerson.getImage().getUrl();
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
String profilePic=personPhoto.substring(0,
personPhoto.length() - 2)
+ ProfilePicSize;
Log.e("GOOGLE", id);
Log.e("GOOGLE", personName);
Log.e("GOOGLE", profilePic);
Log.e("GOOGLE",email);
}
} catch (Exception e) {
e.printStackTrace();
}
}
我已经尝试过相同的代码并且工作正常!
所以,只要确保两件事:
1)在下面的 Google API Console.Link 中注册您的数字签名 .apk 文件的 public 证书:
https://developers.google.com/+/mobile/android/getting-started#step_1_enable_the_google_api
2)确保您已添加 google+ api 访问权限并使用 SHA1 创建了客户端密钥。
休息就好了。
我正在尝试使用 Google+ [=32] 在我的 android 应用程序中包含 Google 登录 =]. 我可以从用户那里获取帐户详细信息,但是一旦登录,当 请求用户名 时,我得到 null使用电话:
Plus.PeopleApi.getCurrentPerson(mGoogleApiClient).getDisplayName()
并且Logcat显示:
BasicNetwork.performRequest: Unexpected response code 403 for https://www.googleapis.com/plus/v1/people/me
虽然我可以使用以下方式获取用户的电子邮件:
Plus.AccountApi.getAccountName(GoogleClient.mGoogleApiClient)
请帮助我发现我的错误
我认为 google+ 应用程序中登录集成的基本步骤已为 you.Here 所知,其余步骤为
第一步-- 在 oncreate
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
第2步-- 在登录按钮中单击侦听器调用此----
private void LoginGoogle(){
int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
if (errorCode != ConnectionResult.SUCCESS) {
GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0).show();
}
else{
//perform login
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
mGoogleApiClient.connect();
}
}
}
第3步---- 在 onActivityresult
if (requestCode == RC_SIGN_IN) {
if (resultCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.reconnect();
}
}
step 4-----
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
if(!mIntentInProgress){
if ( mSignInClicked && result.hasResolution()) {
// The user has already clicked 'sign-in' so we attempt to resolve all
// errors until the user is signed in, or they cancel.
try {
result.startResolutionForResult(this, RC_SIGN_IN);
mIntentInProgress = true;
} 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();
}
}
}
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
mSignInClicked = false;
getProfileInformation();
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
步骤 5-----
private void getProfileInformation(){
try {
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String id=currentPerson.getId();
String personName = currentPerson.getDisplayName();
String personPhoto = currentPerson.getImage().getUrl();
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
String profilePic=personPhoto.substring(0,
personPhoto.length() - 2)
+ ProfilePicSize;
Log.e("GOOGLE", id);
Log.e("GOOGLE", personName);
Log.e("GOOGLE", profilePic);
Log.e("GOOGLE",email);
}
} catch (Exception e) {
e.printStackTrace();
}
}
我已经尝试过相同的代码并且工作正常! 所以,只要确保两件事:
1)在下面的 Google API Console.Link 中注册您的数字签名 .apk 文件的 public 证书:
https://developers.google.com/+/mobile/android/getting-started#step_1_enable_the_google_api
2)确保您已添加 google+ api 访问权限并使用 SHA1 创建了客户端密钥。
休息就好了。