![](/img/trans.png)
[英]how to fetch access token for google api.Authenticating with OAuth 2.0 in android
[英]How to obtain oauth 2.0 token from google plus api in android?
我從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。
沒有關於社交身份驗證的線索,但是要從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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.