問題描述
谷歌播放服務認證(oauth 2.0) (Google play service authentication (oauth 2.0))
I wish to authenticate users of my android application whith the oauth2 protocole, so as to the user don't need to create a login and password for my app.
To do that, I follow the following tutorial : http://android‑developers.blogspot.fr/2013/01/verifying‑back‑end‑calls‑from‑android.html
I've import the google play service sdk sample (in AndroidSDK\extras\google\google_play_services\samples\auth), and I have change the scope value.
However, I don't manage to get authentication token... While I've correctly configure my google console API.
Here is the function calling getToken method :
protected String fetchToken() throws IOException {
try {
return GoogleAuthUtil.getToken(mActivity, mEmail, mScope);
} catch (GooglePlayServicesAvailabilityException playEx) {
// GooglePlayServices.apk is either old, disabled, or not present.
mActivity.showErrorDialog(playEx.getConnectionStatusCode());
} catch (UserRecoverableAuthException userRecoverableException) {
// Unable to authenticate, but the user can fix this.
// Forward the user to the appropriate activity.
mActivity.startActivityForResult(userRecoverableException.getIntent(), mRequestCode);
} catch (GoogleAuthException fatalException) {
onError("Unrecoverable error " + fatalException.getMessage(), fatalException);
}
return null;
}
And here is my scope : audience:server:client_id:MY_CLIENT_ID.apps.googleusercontent.com
When I try that, I get the following exceptions :
08‑16 17:22:08.471: E/TokenInfoTask(16546): Exception:
08‑16 17:22:08.471: E/TokenInfoTask(16546): com.google.android.gms.auth.GoogleAuthException: Unknown
08‑16 17:22:08.471: E/TokenInfoTask(16546): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
參考解法
方法 1:
You should be using Google+ Sign‑In, which Google released in February, because this will get you cross‑client authorization and access to more data that you can use to personalize your app. Behind the scenes, this authorization library uses OAuth 2 to connect to Google services.
Some resources to get you started:
- The Google+ Android Quick Start should get you going with a basic implementation.
- If you have any issues, the most common ones are covered in the Risk Complete Failure blog post Common Problems with Sign‑in on Android
- If you want to see a full front‑end to back‑end integration, you can look at PhotoHunt
- The Google+ tag on Stack Overflow tag has up‑to‑date information and support as well