I'm trying to get an access token on android device in Java.
At first I've created Client ID for installed applications (Android) in Google API Console, and set the package where the access token will be requested, and SHA1 fingerprint.
As I read in OAuth2ForDevices, to obtain an access token, I have to obtain a User code at first. So I tried to send POST request with client_id and scope using Aquery like this:
AQuery aq = new AQuery(activity);
aq.ajax("https://accounts.google.com/o/oauth2/device/code?" +
"scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile" +
"client_id=xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
JSONObject.class,new AjaxCallback<JSONObject>(){
@Override
public void callback(String url, JSONObject traffic_flow, AjaxStatus status) {
publishProgress(traffic_flow.toString());
}
});
The problem is that JSONObject traffic_flow
is allways null. I also tried to get it using this (but I don't thing this is a right way):
authToken = GoogleAuthUtil.getToken(activity, mEmail, "audience:server:client_id:xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com");
where mEmail is the email from the android device, but I got GoogleAuthException
"Unknown". How can I obtain the User code properly?
EDIT:
I was finally able to obtain an auth token using this:
String scope = "audience:server:client_id:xxxxxxxxxxxx.apps.googleusercontent.com";
String token = GoogleAuthUtil.getToken(activity, client.getAccountName(), scope);
where scope is a Client ID for web applications which was generated in Google API Console (afterwards I'm sending the token to my website and verifying it), Client is PlusClient (more in Getting started with Google+).
I've obtained the token in my test application, now the problem is that when I wanted to include the code to my new application, I'm again getting that ugly exception:
com.google.android.gms.auth.GoogleAuthException: Unknown at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
All the client IDs for these applications are in the same project, permissions in manifest should be ok (GET_ACCOUNTS,USE_CREDENTIALS,AUTHENTICATE_ACCOUNTS,INTERNET,ACCESS_NETWORK_STATE
). The only change I've made in the new application is setting scopes when creating PlusClient because it doesn't work without it (don't know why it works without it in my test application)
mPlusClient = new PlusClient.Builder(this, this, this)
.setVisibleActivities("http://schemas.google.com/AddActivity")
.setScopes(Scopes.PLUS_LOGIN, Scopes.PLUS_PROFILE)
.build()
What am I missing?