function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
lichenglicheng 

can not get acces token with error "invalid authorization code"

Hi, all

    Followed with "http://www.salesforce.com/us/developer/docs/api_rest/Content/quickstart_oauth.htm", I'm trying to make a simple client test code to pass. Unfortunately, I could get the authorization token, but when could not get access token, and the response I got when requiring access token is :

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

 

Following is my test code

step1: Registry one remote app, and get customer_key and customer_secret

        customer key: 3MVG9Y6d_Btp4xp4S0UExFwosGGJCLQZq3k8mUbBu6DoVNiWVlxqKMHCzpPi4DHx9b.PsxqacuY5hwpGFlxCM

        customer secret: 7497167051685992293

step2: Open browser, access following url, which includes the required parameters, as below

       https://login.salesforce.com/services/oauth2/authorize?response_type=code&redirect_uri=https%3A%2F%2...

Step3: Get authorization token from the browser address bar

  https://10.111.3.82:8443/?code=aPrxaSyVmC8fBbeBsvplbPnf.sN3TAsh8c2Dnrrxbd203sy7T1m8WbTWtknf_EqNJEZDh...

 

Step4: Try to get access token with following code:
        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","3MVG9Y6d_Btp4xp4S0UExFwosGGJCLQZq3k8mUbBu6DoVNiWVlxqKMHCzpPi4DHx9b.PsxqacuY5hwpGFlxCM");
        post.setParameter("client_secret","7497167051685992293");
        post.setParameter("redirect_uri","https://10.111.3.82:8443");
        post.setParameter("code", "aPrxaSyVmC8fBbeBsvplbPnf.sN3TAsh8c2Dnrrxbd203sy7T1m8WbTWtknf_EqNJEZDh3DYIQ%3D%3D");
        post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        client.executeMethod(post);
        String responseBody = post.getResponseBodyAsString();
        String accessToken = null;
        JSONObject json = null;
        try {
            json = new JSONObject(responseBody);
            accessToken = json.getString("access_token");
        } catch (JSONException e) {
             e.printStackTrace();
        }

 

 Unfortunately, I always get responseBody as below

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

 

Please help!

lichenglicheng

Resloved now. The root cause is the token got from browser address bar always ended with "%3D%3d" which should be encode to "==" first.

Jack Le 5Jack Le 5
Thank you !