2

我从 google API 获取 oauth 2.0 令牌时遇到问题。我目前正在为android编写应用程序,我希望有三种登录方法——通过 facebook(完成)、通过自定义 oauth 2.0 提供程序(也完成)和通过 google plus——这给我带来了很多问题。我需要在设备上获取访问令牌,并将其进一步传递给后端应用程序。

我尝试使用 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 回答