1
我正在嘗試爲我的後端開發具有客戶端Google oAuth2.0授權的應用。我已經完成了Google開發者控制檯所需的所有工作。 只要應用程序請求帶有GoogleAuthUtil.getToken()
的授權令牌,Google就會一直要求離線訪問。我想一勞永逸地給予許可,所以授權過程變得沉默。讓GoogleAuthUtil停止詢問用戶離線訪問
以下是我在我的授權AsyncTask
中使用的代碼。
protected String doInBackground(String... arg0) {
String token = null;
try {
token = GoogleAuthUtil.getToken(mainActivity, mEmail, "oauth2:server:client_id:"+clientId+":api_scope:https://www.googleapis.com/auth/plus.login");
} catch (GooglePlayServicesAvailabilityException playEx) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
playEx.getConnectionStatusCode(),
mainActivity,
1001);
dialog.show();
} catch (UserRecoverableAuthException recoverableException) {
Intent recoveryIntent = recoverableException.getIntent();
mainActivity.startActivityForResult(recoveryIntent,1001);
// Use the intent in a custom dialog or just startActivityForResult.
Log.e("Auth", "Recoverable authentication exception: " + recoverableException.getMessage(), recoverableException);
} catch (GoogleAuthException authEx) {
// This is likely unrecoverable.
Log.e("Auth", "Unrecoverable authentication exception: " + authEx.getMessage(), authEx);
} catch (IOException ioEx) {
Log.i("Auth", "transient error encountered: " + ioEx.getMessage());
}
return token;
}