Я могу получить токен доступа к учетной записи google. Но я не могу получить информацию о пользователе. Я получаю исключение null-указателя. Почему я не понимаю. Ниже я предоставил два метода, с помощью которых мы получаем токен доступа, а также получаем информацию о пользовательском профиле.Невозможно получить информацию о профиле пользователя google +
Было бы здорово, если вы мне поможете.
MainActivity.java
private void tryAuthenticate() {
if (isFinishing()) {
return;
}
mToken = null;
setProgressBarIndeterminateVisibility(true);
AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
try {
mToken =
GoogleAuthUtil.getToken(MainActivity.this, mChosenAccountName, "oauth2:"
+ Scopes.PLUS_LOGIN + " " + "https://www.googleapis.com/auth/plus.login" + " "+" https://www.googleapis.com/auth/plus.profile.agerange.read"+" " +YouTubeScopes.YOUTUBE + " "
+ YouTubeScopes.YOUTUBE_UPLOAD);
} catch (GooglePlayServicesAvailabilityException playEx) {
GooglePlayServicesUtil.getErrorDialog(playEx.getConnectionStatusCode(),
MainActivity.this, REQUEST_GMS_ERROR_DIALOG).show();
} catch (UserRecoverableAuthException userAuthEx) {
// Start the user recoverable action using the intent
// returned by
// getIntent()
startActivityForResult(userAuthEx.getIntent(), REQUEST_AUTHENTICATE);
return false;
} catch (IOException transientEx) {
// TODO: backoff
Log.e(this.getClass().getSimpleName(), transientEx.getMessage());
} catch (GoogleAuthException authEx) {
Log.e(this.getClass().getSimpleName(), authEx.getMessage());
}
return true;
}
@Override
protected void onPostExecute(Boolean hideProgressBar) {
invalidateOptionsMenu();
if (hideProgressBar) {
setProgressBarIndeterminateVisibility(false);
}
if (mToken != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
saveAccount();
}
});
}
loadData();
}
};
task.execute((Void) null);
}
private void loadProfile() {
new AsyncTask<Void, Void, Person>() {
@Override
protected Person doInBackground(Void... voids) {
GoogleCredential credential = new GoogleCredential();
credential.setAccessToken(mToken);
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
Plus plus =
new Plus.Builder(httpTransport, jsonFactory, credential).setApplicationName(
Constants.APP_NAME).build();
try {
return plus.people().get("me").execute(); //here am getting null pointer exception
} catch (final GoogleJsonResponseException e) {
if (401 == e.getDetails().getCode()) {
Log.e(this.getClass().getSimpleName(), e.getMessage());
GoogleAuthUtil.invalidateToken(MainActivity.this, mToken);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
tryAuthenticate();
}
}, mCurrentBackoff * 1000);
mCurrentBackoff *= 2;
if (mCurrentBackoff == 0) {
mCurrentBackoff = 1;
}
}
} catch (final IOException e) {
Log.e(this.getClass().getSimpleName(), e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Person me) {
mUploadsListFragment.setProfileInfo(me);
}
}.execute((Void) null);
}
Я видел где-то в примере «я» означает аутентифицированный пользователь. почему он не распознает пользователя, который был аутентифицирован? –
в любом случае спасибо за ссылку. –