我正在尝试根据这篇文章:为您的应用程序启用服务器端访问获取Google+认证代码。然而,每当我运行我的应用程序并尝试获取代码时,LogCat 中会显示以下内容:
07-23 23:42:31.760: E/getAccessToken()(11114): [ERROR] GoogleAuthException: com.google.android.gms.auth.GoogleAuthException: Unknown
我浏览了很多内容,从下面的代码中可以看出我已经正确设置了一切。有什么想法为什么它没有获得授权令牌?
顺便说一下,我在API控制台中设置了一个项目,将应用程序和Web应用程序都配置为OAuth,并且我正在使用服务器(而不是应用程序)ID作为范围字符串。我还遵循了上面链接中的示例,并且应用程序运行良好,允许您登录/注销,但我只是无法获取授权代码。
private String getAccessToken() {
String scopesString = Scopes.PLUS_LOGIN + " " + Scopes.PLUS_PROFILE;
String scope = "oauth2:server:client_id:" + SERVER_CLIENT_ID + ":api_scope:" + scopesString;
Bundle appActivities = new Bundle();
appActivities.putString(GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES,
"http://schemas.google.com/AddActivity");
String code = null;
try {
code = GoogleAuthUtil.getToken(
this, // Context context
mPlusClient.getAccountName(), // String accountName
scopes, // String scope
appActivities // Bundle bundle
);
} catch (IOException transientEx) {
// network or server error, the call is expected to succeed if you try again later.
// Don't attempt to call again immediately - the request is likely to
// fail, you'll hit quotas or back-off.
Log.e("getAccessToken()", "[ERROR] IOException: " + transientEx);
return null;
} catch (UserRecoverableAuthException e) {
// Recover
Log.e("getAccessToken()", "[ERROR] UserRecoverableAuthException: " + e);
code = null;
} catch (GoogleAuthException authEx) {
// Failure. The call is not expected to ever succeed so it should not be
// retried.
Log.e("getAccessToken()", "[ERROR] GoogleAuthException: " + authEx);
return null;
} catch (Exception e) {
Log.e("getAccessToken()", "[ERROR] Exception: " + e);
throw new RuntimeException(e);
}
return code;
}