1

I am able to get the "code" in my redirect Url but unable to get access token.

I tried sending POST request using POSTMAN and also from java code but "invalid_grant" error i got.

Response {"error_description":"invalid authorization code","error":"invalid_grant"}

HttpClient client = new HttpClient();
        PostMethod post = new PostMethod("https://login.salesforce.com/services/oauth2/token");
        post.setParameter("grant_type", "authorization_code");
        post.setParameter("client_id", "MASKED");
        post.setParameter("client_secret", "MASKED");
        post.setParameter("redirect_uri", "https://www.facebook.com/");
        post.setParameter("code", "xxxxxxxxxxxxxxxxxxxxxxxxxxx");

What I am missing in the above code ?

9
  • Never post your client secret on a public forum
    – Eric
    Commented Aug 8, 2015 at 20:11
  • possible duplicate of Failed refresh of access tokens on some accounts
    – Eric
    Commented Aug 8, 2015 at 20:11
  • Sure, I forgot to remove secret key. @Eric I have gone through the Failed refresh of access tokens on some accounts question before posting this but the solution mentioned did not help me.
    – San
    Commented Aug 8, 2015 at 20:22
  • One more thing I want to add : I am able to get access token using CURL command where grant_type=password
    – San
    Commented Aug 8, 2015 at 20:25
  • 1
    Check my answer here and test it out as it works properly. Then refactor to get what you want testing to see where you break it if at all.. salesforce.stackexchange.com/questions/83913/…
    – Eric
    Commented Aug 8, 2015 at 20:35

1 Answer 1

0

Instead of "https://login.salesforce.com/services/oauth2/token" your should send your request to "https://login.salesforce.com/services/oauth2/authorize" so your code should look like

HttpClient client = new HttpClient();
        PostMethod post = new PostMethod("https://login.salesforce.com/services/oauth2/authorize");
        post.setParameter("grant_type", "authorization_code");
        post.setParameter("client_id", "MASKED");
        post.setParameter("client_secret", "MASKED");
        post.setParameter("redirect_uri", "https://www.facebook.com/");
        post.setParameter("code", "xxxxxxxxxxxxxxxxxxxxxxxxxxx");

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .