この記事に基づいて Google+ 認証コードを取得しようとしています: Server-side access for your app。ただし、アプリを実行してコードを取得しようとすると、LogCat で次のようになります。
07-23 23:42:31.760: E/getAccessToken()(11114): [ERROR] GoogleAuthException: com.google.android.gms.auth.GoogleAuthException: Unknown
私は多くのものを閲覧しましたが、以下のコードですべてが正しくセットアップされていることがわかります。認証トークンを取得していない理由はありますか?
参考までに、oauth 用に構成されたアプリと Web アプリケーションの両方を含む API コンソールに 1 つのプロジェクトをセットアップし、スコープ文字列に (アプリではなく) サーバー 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;
}