2

Google API から oauth 2.0 トークンを取得する際に問題があります。私は現在、android 用のアプリを作成しています。Facebook 経由 (完了)、カスタム oauth 2.0 プロバイダー経由 (これも完了)、Google プラス経由の 3 つのサインイン方法が必要です。これは私にとって多くの問題を引き起こします。デバイスでアクセス トークンを取得し、それをさらにバックエンド アプリケーションに渡す必要があります。

GoogleApiClient を使用してみました (PlusClient は非推奨です - http://developer.android.com/reference/com/google/android/gms/plus/PlusClient.html経由) が、 getAccessToken などのメソッドが表示されません。

現在、socialauth ライブラリを使用しようとしていますが、ドキュメントが不足しているため行き詰まっています (ここにいくつかのコードがあります)。

private SocialAuthAdapter mSocialAdapter;
... in onCreate()
mSocialAdapter = new SocialAuthAdapter(new GooglePlusSocialAuthListener());
try {
    mSocialAdapter.addConfig(Provider.GOOGLEPLUS, key_from_google_developers_console, secret, 
    "https://www.googleapis.com/auth/userinfo.profile+https://www.googleapis.com/auth/userinfo.email");
    mSocialAdapter.addCallBack(Provider.GOOGLEPLUS, 
   "http://localhost:3000/api/auth/gplus/callback");
}
catch (Exception e) {
   e.printStackTrace();
}

mSocialAdapter.authorize(LoginActivity.this, Provider.GOOGLEPLUS);
Log.e("TOKEN HERE ", mSocialAdapter.getCurrentProvider().getAccessGrant().getKey());

このトークンを取得するのを手伝ってくれる人はいますか? socialauth 経由か GoogleApiClient 経由かは関係ありません。

4

1 に答える 1

5

ソーシャル認証についての手がかりはありませんが、Google Play サービスからトークンを取得するには、実際には別のクラス GoogleAuthUtil を使用します。

私はこれの要点をhttps://gist.github.com/ianbarber/9607551に置きました- 関連する部分:

String scopes = "oauth2:profile email"; // magic string! oauth2: followed by space sep scopes.
String token = null;
try {
    token = GoogleAuthUtil.getToken(getApplicationContext(), accountName, scopes);
} catch (IOException e) {
    Log.e(TAG, e.getMessage());
} catch (UserRecoverableAuthException e) {
    startActivityForResult(e.getIntent(), REQ_SIGN_IN_REQUIRED);
} catch (GoogleAuthException e) {
    Log.e(TAG, e.getMessage());
}
return token;

アカウント名は、GoogleApiClient Plus.Account API http://developer.android.com/reference/com/google/android/gms/plus/Account.htmlから取得できます。

于 2014-03-17T20:28:32.637 に答える