问题
In my android application i need to integrate Google+ sign in. I have tried by this method and i can able to get access token successfully. But i can't able to get email address in my access token, while passing into my web service.
I have Google it for past three days and i have tried in so many ways i didn't find solution for my issue. I have tried all scopes i didn't get email address in my access token.
String mScope = "oauth2:" + Scopes.PLUS_LOGIN + " " + Scopes.PLUS_ME;
String exchangeCode = GoogleAuthUtil.getToken(getApplicationContext(), Plus.AccountApi.getAccountName(mGoogleApiClient), mScope);
Here im getting access token and not email address in my token
String exchangeCode = GoogleAuthUtil.getToken(
SigninScreen.this,
Plus.AccountApi.getAccountName(mGoogleApiClient) + "",
"oauth2:"
+ Scopes.PLUS_ME + " "
+ "https://www.googleapis.com/auth/plus.login" + " "
+ "https://www.googleapis.com/auth/plus.me" + " "
+ "https://www.googleapis.com//auth/plus.profile.emails.read" + " "
+ "https://www.googleapis.com/auth/userinfo.profile");
here im getting error like this
com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission
by this way also i tried
accessToken = GoogleAuthUtil.getToken(
MainActivity.this,
Plus.AccountApi.getAccountName(mGoogleApiClient) + "",
"oauth2:"
+ Scopes.PROFILE + " "
+ "https://www.googleapis.com/auth/plus.login" + " "
+ "https://www.googleapis.com/auth/plus.profile.emails.read");
Im getting error by using this method
Client error response [url] https://www.googleapis.com/plus/v1/people/me?prettyPrint=false [status code] 403 [reason phrase] Forbidden
Can anyone tell me what am I doing wrong? I need to get the access token from Google Plus and pass the token to my webservice and get the details.
Thanks a lot in Advance.
回答1:
Try this...
in OnCreate..
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
Add this three methods in Activity... Call sign in and sign out when required
private void signInWithGplus() {
Log.i("call", "signinwithgoogle");
mGoogleApiClient.connect();
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
private void resolveSignInError() {
Log.i("call", "resolvesigninerror");
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
static void signOutFromGplus() {
Log.i("call", "signoutfromgoogle");
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
// Toast.makeText(getApplicationContext(), "sign out from google",
// Toast.LENGTH_SHORT).show();
storeUserData.setBoolean(AppConstants.KEY_IS_LOGIN, false);
updateUI(false);
}
}
Also add to authenticate. By this method you will get whole profile info:
private void getProfileInformation() {
try {
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi
.getCurrentPerson(mGoogleApiClient);
googleFirstName = currentPerson.getDisplayName();
googleImage = currentPerson.getImage().getUrl();
// String personGooglePlusProfile = currentPerson.getUrl();
googleEmailId = Plus.AccountApi
.getAccountName(mGoogleApiClient);
googleId = currentPerson.getId();
Log.i("googleId", googleId);
JSONObject fullname = new JSONObject(currentPerson.getName()
+ "");
googleFirstName = fullname.getString("familyName");
googleLastName = fullname.getString("givenName");
if (currentPerson.getGender() == 0)
googleGender = "female";
else if (currentPerson.getGender() == 1)
googleGender = "male";
else
googleGender = "other";
// by default the profile url gives 50x50 px image only
// we can replace the value with whatever dimension we want by
// replacing sz=X
googleImage = googleImage
.substring(0, googleImage.length() - 2)
+ PROFILE_PIC_SIZE;
new GetGoogleAuthTask().execute();
// new LoadProfileImg(null).execute(personPhotoUrl);
} else {
// Toast.makeText(getApplicationContext(),
// "Person information is null", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
SharedPreferences SharedPreference;
Editor editor;
private class GetGoogleAuthTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String token = null;
try {
token = GoogleAuthUtil.getToken(RegisterActivity.this,
Plus.AccountApi.getAccountName(mGoogleApiClient),
"oauth2:" + Scopes.PLUS_LOGIN + " " + Scopes.PLUS_ME);
// Change the permissions as per your need.
} catch (IOException transientEx) {
// Network or server error, try later
Log.e(TAG, transientEx.toString());
} catch (UserRecoverableAuthException e) {
// Recover (with e.getIntent())
Log.e(TAG, e.toString());
// Intent recover = e.getIntent();
// startActivityForResult(recover, REQUEST_CODE_TOKEN_AUTH);
} catch (GoogleAuthException authEx) {
// The call is not ever expected to succeed
// assuming you have already verified that
// Google Play services is installed.
Log.e(TAG, authEx.toString());
}
return token;
}
@Override
protected void onPostExecute(String token) {
if (token != null) {
googleToken = token;
// Log.i(TAG, "Access token retrieved:" + token);
SharedPreference = getApplicationContext()
.getSharedPreferences("TokenPreference", 0);
editor = SharedPreference.edit();
editor.putString("access_token", token);
editor.commit();
}
Log.i("GooGle", "called");
loginWithGoogleData();
storeUserData.setBoolean(AppConstants.KEY_IS_LOGIN, true);
}
}
and at last in onactivity result
@Override
public void onActivityResult(int requestCode, int responseCode, Intent data) {
super.onActivityResult(requestCode, responseCode, data);
uiHelper.onActivityResult(requestCode, responseCode, data);
/******** GOOGLE CODE START **************/
if (requestCode == RC_SIGN_IN) {
if (responseCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
/******** GOOGLE CODE END **************/
}
Don't forget to add in permissions in manifest...
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
回答2:
Finally i got a solution after 3 days.
i have changed the scope. now its working fine. i can able get all details even email id from access token.
protected String doInBackground(String... args) {
String token = null;
String scope = "oauth2:" + Scopes.PLUS_LOGIN + " " + "https://www.googleapis.com/auth/userinfo.email" + " https://www.googleapis.com/auth/plus.profile.agerange.read";
try {
token = GoogleAuthUtil.getToken(
SigninScreen.this,
Plus.AccountApi.getAccountName(mGoogleApiClient),
scope);
System.out.println("OKAY!"+token);
} catch (IOException transientEx) {
// Network or server error, try later
Log.e(TAG, transientEx.toString());
} catch (UserRecoverableAuthException e) {
// Recover (with e.getIntent())
Log.e(TAG, e.toString());
Intent recover = e.getIntent();
startActivityForResult(recover, 125);
} catch (GoogleAuthException authEx) {
// The call is not ever expected to succeed
// assuming you have already verified that
// Google Play services is installed.
Log.e(TAG, authEx.toString());
}
return token;
来源:https://stackoverflow.com/questions/31583676/google-plus-intergration-using-access-token