Reputation: 63
I want to obtain an Access Token is for my application using the Authorization Code it just acquired. I am using this code
DefaultHttpClient client = new DefaultHttpClient();
URI uri = new URIBuilder().setScheme("https")
.setHost("www.linkedin.com")
.setPath("/uas/oauth2/accessToken")
.setParameter("grant_type", "authorization_code")
.setParameter("code", code)
.setParameter("redirect_uri", "http://localhost:9090/ConnectSocialMedia/callBack.jsp")
.setParameter("client_id", CONSUMER_KEY_OPTION)
.setParameter("client_secret", CONSUMER_SECRET_OPTION)
.build();
HttpPost post = new HttpPost(uri);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
HttpResponse response2 = client.execute(post);
System.out.println("Final Response------------>"+response2);
But I am getting HTTP/1.1 400 Bad Request. What am I doing wrong?
Any help would be really appreciated!
Upvotes: 0
Views: 83
Reputation: 53888
You're adding the parameters to the token endpoint as query parameters in the URL but they should be passed in the POST parameters. You should use something like:
DefaultHttpClient client = new DefaultHttpClient();
URI uri = new URIBuilder().setScheme("https")
.setHost("www.linkedin.com")
.setPath("/uas/oauth2/accessToken")
.build();
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("grant_type", "authorization_code"));
nvps.add(new BasicNameValuePair("code", code));
nvps.add(new BasicNameValuePair("redirect_uri", "http://localhost:9090/ConnectSocialMedia/callBack.jsp"));
nvps.add(new BasicNameValuePair("client_id", CONSUMER_KEY_OPTION));
nvps.add(new BasicNameValuePair("client_secret", CONSUMER_SECRET_OPTION));
HttpPost post = new HttpPost(uri);
post.setEntity(new UrlEncodedFormEntity(nvps));
HttpResponse response2 = client.execute(post);
System.out.println("Final Response------------>"+response2);
Upvotes: 1