Reputation: 1273
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?
Upvotes: 0
Views: 4469
Reputation: 5349
This is the code I use for getting an OAuth 2.0 token for use with the Google Coordinate api, I run it within an ASyncTask. I've not included the part for generating DESIRED_USER as that uses startActivityForResult() on some AccountManager intent. Hopefully you can use this.
You should replace SCOPE with yours, which i think should be oauth2:https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile
import android.accounts.Account;
import android.accounts.AccountManager;
final String SCOPE = "oauth2:https://www.googleapis.com/auth/coordinate";
AccountManager accountManager = AccountManager.get(context);
//Kill Current token
accountManager.invalidateAuthToken("com.google",authManager.getToken());
//Fetch userAccount
Account userAccount = null;
for(Account account : accountManager.getAccounts())
if(account.name.equals(DESIRED_USER))
{
try {
return accountManager.blockingGetAuthToken(userAccount, SCOPE, false);
} catch (Exception e) {
System.out.println("Error: "+e.toString());
e.printStackTrace();
}
}
return null;
Upvotes: 1
Reputation: 2549
If you have to use this API then you should use POST version of AQuery and pass the POST parameters properly as below. This API OAuth2ForDevices is meant for resources constrained devices where the user has another way of authorizing your app.
params.put("scope", "your scopes");
params.put("client_id", "your client id");
AQuery aq = new AQuery(activity);
aq.ajax("https://accounts.google.com/o/oauth2/device/code", params, JSONObject.class,
new AjaxCallback<JSONObject>() {
@Override
public void callback(String url, JSONObject traffic_flow, AjaxStatus status) {
publishProgress(traffic_flow.toString());
}
});
However, if your requirement is to use regular OAuth2 with Android on say an Android phone with regular input capabilities then the standard OAuth mechanism with Android is this.
Upvotes: 1