0

Web サーバーで accessToken と RefreshToken と交換できる認証トークンを Android で取得しようとしています。私はこのドキュメントに従っています。
具体的には、セクション - Android アプリが Web バックエンドのオフライン アクセスを取得する

私のコード:

String WEB_APP_CLIENT_ID = "***.apps.googleusercontent.com";
String scopes = "oauth2:server:client_id:" + WEB_APP_CLIENT_ID+":api_scope:" + Scopes.PLUS_LOGIN + " https://www.googleapis.com/auth/plus.profile.emails.read";
//String this_scope_works_but_gives_access_token = "oauth2:" + Scopes.PLUS_LOGIN + " https://www.googleapis.com/auth/plus.profile.emails.read";
try {
    String authToken = GoogleAuthUtil.getToken(AttachEmailActivity.this, account,scopes);
} catch (GoogleAuthException e) {
    e.printStackTrace();
}

しかし、それは常に私に次の例外を与えています:

com.google.android.gms.auth.GoogleAuthException: Unknown
com.google.android.gms.auth.GoogleAuthUtil.zza(Unknown Source)
com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
app.activities.AttachEmailActivity$1.run(AttachEmailActivity.java:437)

私は、まだ失敗している同様の問題に関するほとんどすべてのソリューションを読んで試しました。Web クライアント ID、サーバー アプリケーション ID、Android クライアント ID、その他のクライアント ID など、さまざまなクライアント ID をすべて使用してみました。

実際、同じ Web クライアント ID を使用して Java アプリケーションの外部で REST API を使用したところ、必要な認証コードを取得できました。

https://accounts.google.com/o/oauth2/auth?response_type=code&client_id= ****.apps.googleusercontent.com&redirect_uri= http://localhost&scope=https://www.googleapis.com/auth/plus .login&access_type=オフライン

しかし、Java アプリケーションでは、「不明な」例外しか発生しません。グーグルはより多くの情報を提供できませんか?何が不明ですか?com.google.android.gms.auth パッケージ コードを取得し、例外がスローされている場所を確認する方法はありますか?

資格情報ダッシュボードのビューは次のとおりです

4

2 に答える 2

0

私の場合、ユーザーの許可を求めるために UserRecoverableAuthException をキャッチする必要があります

String scopes = "oauth2:server:client_id:"
        +
        getResources().getString(R.string.google_auth_client_id)
        +
        ":api_scope:" + Scopes.PLUS_LOGIN;
try {
    String token = GoogleAuthUtil.getToken(MyActivity.this,
            Plus.AccountApi.getAccountName(googleApiClient), scopes);
    subscriber.onNext(token);
} catch (UserRecoverableAuthException e) {
    startActivityForResult(e.getIntent(), 888);
    e.printStackTrace();
} catch (GoogleAuthException | IOException e) {
    e.printStackTrace();
}

次に、onActivityResult 内でトークンを取得します

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 888) {
        if (data != null && resultCode == Activity.RESULT_OK) {
            Bundle extra = data.getExtras();
            String token = extra.getString(getResources()
                    .getString("authtoken"));
            // Do something with your token
        }

        if (!googleApiClient.isConnecting()) {
            googleApiClient.connect();
        }
    }
}
于 2015-11-05T05:25:07.430 に答える