我正在尝试在我的 Android 应用程序中检索已登录用户的 Google Plus 访问令牌。我正在使用以下代码执行此操作,该代码遵循此处、此处和此处的建议:
private class RetrieveTokenTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
int REQUEST_CODE_TOKEN_AUTH = 466453; // Any number, not really using it anyway.
String accountName = params[0];
String scopes = "oauth2:" + Plus.SCOPE_PLUS_LOGIN;
String token = null;
try {
token = GoogleAuthUtil.getToken(getActivity(), accountName, scopes);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
} catch (UserRecoverableAuthException e) {
startActivityForResult(e.getIntent(), REQUEST_CODE_TOKEN_AUTH);
} catch (GoogleAuthException e) {
Log.e(TAG, e.getMessage());
}
return token;
}
}
我这样称呼它:
@Override
public void onConnected(Bundle connectionHint) {
mSignInClicked = false;
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
try {
String accessToken = (new RetrieveTokenTask()).execute(Plus.AccountApi.getAccountName(mGoogleApiClient)).get();
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
}
我在我的 AndroidManifest 中拥有以下权限(除其他外):
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
问题是调用时GoogleAuthUtil.getToken()
,没有任何进展,它只会在那里冻结。我怎样才能解决这个问题?