I've found that I can authenticate via OAuth 2.0 when my redirect uri is "urn:ietf:wg:oauth:2.0:oob", BUT the user is forced to copy the code, then go back one activity and paste it into a field. I want the experience to be more elegant than that. When the redirect uri is "http://localhost", (even though an access code is returned) I'm unable to exchange it for an access token to the api. Here's my exchange code:
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
0);
nameValuePairs.add(new BasicNameValuePair("client_id",
OAuth2ClientCredentialsMark1.CLIENT_ID));
nameValuePairs.add(new BasicNameValuePair("client_secret",
OAuth2ClientCredentialsMark1.CLIENT_SECRET));
nameValuePairs.add(new BasicNameValuePair("code", accessCode));
nameValuePairs.add(new BasicNameValuePair("grant_type",
"authorization_code"));
nameValuePairs.add(new BasicNameValuePair("redirect_uri",
OAuth2ClientCredentialsMark1.REDIRECT_URI));
//"http://localhost"
String url = "https://accounts.google.com/o/oauth2/token";
//url += URLEncodedUtils.format(nameValuePairs, "utf-8");
Log.d("print", url);
HttpPost hPost = new HttpPost(
url);
hPost.setHeader("content-type", "application/x-www-form-urlencoded");
hPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
This code always returns {"error" : "invalid_grant"} What gives?
My app is based of the sample @ https://github.com/ddewaele/LatitudeOAuth2Sample and I've been following the tutorial @ http://code.google.com/apis/accounts/docs/OAuth2InstalledApp.html