【发布时间】:2014-12-27 22:27:20
【问题描述】:
我正在尝试为我的后端开发一个具有客户端 Google oAuth2.0 授权的应用程序。我已经在 Google 开发者控制台上完成了所有需要的工作。
每当应用使用GoogleAuthUtil.getToken() 请求授权令牌时,Google 都会不断要求离线访问。我想一劳永逸地授予权限,因此授权过程变得无声。
这是我在授权中使用的代码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;
}
【问题讨论】:
标签: android google-app-engine google-oauth