user388238
user388238

Reputation: 23

Error while requesting an access token. No accessTokenResponse object recieved, maybe a non HTTP 200 received?

I was following this tutorial: https://developers.docusign.com/esign-rest-api/code-examples/config-and-auth (I used java)

In the third step  I dont know how to get the code that is sent back form DocuSign as query param in the redirect uri:

// Java request auth token
**String code = "{ENTER_AUTH_CODE_FROM_PREVIOUS_STEP}";**
// assign it to the token endpoint
apiClient.getTokenEndPoint().setCode(code);

// optionally register to get notified when a new token arrives
apiClient.registerAccessTokenListener(new AccessTokenListener() {
    @Override
    public void notify(BasicOAuthToken token) {
        System.out.println("Got a fresh token: " + token.getAccessToken());
    }
});

// following call exchanges the authorization code for an access code and updates 
// the `Authorization: bearer <token>` header on the api client
apiClient.updateAccessToken();

I get an error saying the requested access token is null. Below is the error:

Error while requesting an access token. No accessTokenResponse object received, maybe a non HTTP 200 received?

Has anybody ever got the same one or could maybe someone tell me how to fix it.

Upvotes: 2

Views: 784

Answers (1)

Amit K Bist
Amit K Bist

Reputation: 6818

I ran below code and its working fine for me, after getting code from the previous step in the URL:

public static void main(String[] args) {

                String IntegratorKey = "[Your_Integrator_Key]";

                String ClientSecret = "[Your_Secret_Key]";

                String RedirectURI = "https://www.getpostman.com/oauth2/callback";//This REDIRECT_URI should match whats configured with IntegratorKey in your Sandbox account

                String AuthServerUrl = "https://account-d.docusign.com";

                String RestApiUrl = "https://demo.docusign.net/restapi";

                ApiClient apiClient = new ApiClient(AuthServerUrl, "docusignAccessCode", IntegratorKey, ClientSecret);

                apiClient.setBasePath(RestApiUrl);

                apiClient.configureAuthorizationFlow(IntegratorKey, ClientSecret, RedirectURI);

                Configuration.setDefaultApiClient(apiClient);

                String code = "{ENTER_AUTH_CODE_FROM_PREVIOUS_STEP}";
                apiClient.getTokenEndPoint().setCode(code);

                apiClient.registerAccessTokenListener(new AccessTokenListener() {
                    @Override
                    public void notify(BasicOAuthToken token) {
                        System.out.println("Got a fresh token: " + token.getAccessToken());
                    }
                });

                apiClient.updateAccessToken();
    }

To get the code returned by DocuSign from browser, you need to have a WEBApp to which DocuSign will redirect the browser, this same callback URL should be configured in your DS Sandbox's REDIRECT_URI, for instance if you WEBApp callback URL is http://locahost:8080/docusignapp/callback, then this should be added in REDIRECT_URI in IntegratorKey and same needs to be added when calling DS URL to authenticate a user. Once DS authenticates the user, it will redirect the browser to your APP url. On hitting your WEBApp then you need to read the URL and strip off the code part using Javascript, then run the step2 to get the access Token. A sample JS code to strip the code part is:

var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
  hash = hashes[i].split('=');
  vars.push(hash[0]);
  vars[hash[0]] = hash[1];
}

var authCode = vars["code"];

In the example which you share they did it using Standalone code, where you are manually copying the code part from the URL after authentication and running the step2.

Upvotes: 2

Related Questions