2

After Step 1 of the oauth2 dance I can retreive a code in the redirect URL. This works fine. But then,

I've got an 'com.google.api.client.http.HttpResponseException: 400 Bad Request' error

trying to get the accessTokenResponse. Any idea why ?

AuthorizationCodeGrant request = new AuthorizationCodeGrant(new NetHttpTransport(),
                                new JacksonFactory(),
                                OAuth2ClientCredentials.ACCESS_TOKEN_URL,
                                OAuth2ClientCredentials.CLIENT_ID, 
                                OAuth2ClientCredentials.CLIENT_SECRET,
                                code,
                                OAuth2ClientCredentials.REDIRECT_URI);

                        try {
                            AccessTokenResponse accessTokenResponse = request.execute();
                            CredentialStore credentialStore = new SharedPreferencesCredentialStore(prefs);
                            credentialStore.write(accessTokenResponse );
                        } catch (IOException e) {
                            Log.e(TAG, "error= "+e);
                            e.printStackTrace();
                        }

this is the line that triggers the error :

AccessTokenResponse accessTokenResponse = request.execute();

I'm using 'com.google.api.client.auth.oauth2.draft10.AccessTokenRequest.AuthorizationCodeGrant'

Should I use somehting else maybe ? any suggestion ?

Hubert
  • 16,012
  • 18
  • 45
  • 51
  • You could try manually requesting an authorization token with the code returned and see if that works. See http://stocktwits.com/developers/docs/api#oauth-token-docs for an example. – AdamB Sep 28 '12 at 18:17

2 Answers2

1

See Step 5 of the authorization flow or the oauth/token end-point.

Seems like you're missing the grant_type.

Sayem Khan
  • 532
  • 7
  • 21
  • I can't use AuthorizationCodeGrant then (as it does not need a TOKEN_GRANT_TYPE). What would u suggest to use instead ? I'm wondering if you have any preference in terms of external library? – Hubert Sep 29 '12 at 06:59
  • You could just try doing it manually or via curl on your command line to get the access_token or even do it client-side, if all else fails. – Sayem Khan Oct 01 '12 at 21:48
1

This looks to be working :

// Create a new HttpClient and Post Header
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost(OAuth2ClientCredentials.ACCESS_TOKEN_URL);

                        try {
                            // Add your data
                            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
                            nameValuePairs.add(new BasicNameValuePair("client_id", OAuth2ClientCredentials.CLIENT_ID));
                            nameValuePairs.add(new BasicNameValuePair("client_secret", OAuth2ClientCredentials.CLIENT_SECRET));
                            nameValuePairs.add(new BasicNameValuePair("code", code));
                            nameValuePairs.add(new BasicNameValuePair("grant_type", OAuth2ClientCredentials.TOKEN_GRANT_TYPE));
                            nameValuePairs.add(new BasicNameValuePair("redirect_uri", OAuth2ClientCredentials.REDIRECT_URI));
                            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                            Log.i(TAG, "httppost= "+inputStreamToString(httppost.getEntity().getContent()));

                            // Execute HTTP Post Request
                            HttpResponse response = httpclient.execute(httppost);
                            Log.i(TAG, "response= "+inputStreamToString(response.getEntity().getContent()));

                        } catch (ClientProtocolException e) {
                            Log.e(TAG, "error= "+e);
                        } catch (IOException e) {
                            Log.e(TAG, "error= "+e);
                        }

The redirect url contains the access token I need.

Hubert
  • 16,012
  • 18
  • 45
  • 51