如何从android中的google plus api获取oauth 2.0令牌?

Zka*_*art 2 api android oauth google-plus oauth2client

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

我尝试使用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());
Run Code Online (Sandbox Code Playgroud)

有人可以帮我拿到这个令牌吗?无论是通过socialauth还是通过GoogleApiClient都没关系.

Ian*_*ber 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;
Run Code Online (Sandbox Code Playgroud)

帐户名称可以从GoogleApiClient Plus.Account API 获取http://developer.android.com/reference/com/google/android/gms/plus/Account.html