简体   繁体   English

如何从Android中的Google Plus API获取OAuth 2.0令牌?

[英]How to obtain oauth 2.0 token from google plus api in android?

I have a problem with obtaining oauth 2.0 token from google API. 我从Google API获取oauth 2.0令牌时遇到问题。 I am currently writing app for android , where I want to have three methods of signing in - via facebook (done), via custom oauth 2.0 provider (also done) and via google plus - it makes many problems for me. 我目前正在为android开发应用程序,在这里我想使用三种登录方法-通过facebook(完成),通过自定义oauth 2.0提供程序(也已完成)和通过google plus-它给我带来了很多问题。 I need to get access token on the device, and pass it further to backend application. 我需要在设备上获取访问令牌,然后将其进一步传递给后端应用程序。

I tried using GoogleApiClient (PlusClient is deprecated - via http://developer.android.com/reference/com/google/android/gms/plus/PlusClient.html ), but cannot see a method like getAccessToken or something similar. 我尝试使用GoogleApiClient(不推荐使用PlusClient-通过http://developer.android.com/reference/com/google/android/gms/plus/PlusClient.html ),但是看不到诸如getAccessToken之类的方法。

Currently i try to use socialauth library, but I'm stuck due to lack of documentation (here is some code) 目前,我尝试使用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());

Can anybody help me to get this token? 有人可以帮助我获得此令牌吗? It doesn't matter if it is via socialauth or via GoogleApiClient. 不管是通过socialauth还是通过GoogleApiClient。

No clue on social auth, but to get the token from Google Play services you actually use another class, GoogleAuthUtil. 没有关于社交身份验证的线索,但是要从Google Play服务获取令牌,您实际上使用的是另一个类GoogleAuthUtil。

I put a gist of this on https://gist.github.com/ianbarber/9607551 - the relevant part: 我将其要点放在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;

The account name can be got from the GoogleApiClient Plus.Account API http://developer.android.com/reference/com/google/android/gms/plus/Account.html 可以从GoogleApiClient Plus.Account API 获取帐户名称http://developer.android.com/reference/com/google/android/gms/plus/Account.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM