GoogleAuthUtil.getToken() 块
GoogleAuthUtil.getToken() blocks
我正在尝试为我的 Android 应用程序中的登录用户检索 Google Plus 访问令牌。我正在使用以下代码执行此操作,该代码遵循 here, here and here 提出的建议:
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());
}
}
我的Android清单(以及其他)中有以下权限:
<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()
时没有任何进展,只会卡在那里。我该如何解决这个问题?
The problem is that when calling GoogleAuthUtil.getToken(), there is
no progress being made and it will just freeze there.
因为您正在调用 get()
方法 AsyncTask
,这使得 Main UI 线程等待 doInBackground
计算未完成
How can I fix this?
调用execute
方法执行AsyncTask
,使用onPostExecute得到accessToken
我正在尝试为我的 Android 应用程序中的登录用户检索 Google Plus 访问令牌。我正在使用以下代码执行此操作,该代码遵循 here, here and here 提出的建议:
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());
}
}
我的Android清单(以及其他)中有以下权限:
<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()
时没有任何进展,只会卡在那里。我该如何解决这个问题?
The problem is that when calling GoogleAuthUtil.getToken(), there is no progress being made and it will just freeze there.
因为您正在调用 get()
方法 AsyncTask
,这使得 Main UI 线程等待 doInBackground
计算未完成
How can I fix this?
调用execute
方法执行AsyncTask
,使用onPostExecute得到accessToken