Issue with generating Access Tokens in sandbox environment

Hi, I have an issue trying to get an access token from https://sandbox.dwolla.com/oauth/v2/token

According to the docs, I use my key i get from the dashboard for client_id, and secret as client_secret.
I set the content-type to application/x-www-form-urlencoded.

However the response from the serever is as follows:

{“error”:“access_denied”,“error_description”:“Invalid application credentials.”}

Ive tried this several ways including using client_id and client_secret as header parameters… i tried setting them as a json obejct in the body… but either way doesnt seem to work.

Here is the code i used in java:

        URL url = new URL("https://sandbox.dwolla.com/oauth/v2/token");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        
        
        conn.setDoInput(true);
        conn.setDoOutput(true);
        
        String data = "";
            
            
            
            
           
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("client_id", "<CLIENT ID>");
        jsonObj.put("client_secret", "<CLIENT SECRET>");
        jsonObj.put("grant_type", "client_credentials");
        
        data = jsonObj.toString();
            
            
           
            byte[] outputInBytes = data.getBytes("UTF-8");
            OutputStream os = conn.getOutputStream();
            os.write( outputInBytes );    
            os.close();
           
            
            System.out.println("Message:" + conn.getResponseMessage());
            //System.out.println("Location:" + conn.getHeaderField("Location"));
            
            BufferedReader br = new BufferedReader(new InputStreamReader(
			(conn.getInputStream())));

	String output;
	System.out.println("Output from Server .... \n");
	while ((output = br.readLine()) != null) {
		System.out.println(output);
	}

	conn.disconnect();

Hey sanziosan,

I think the problem you’re running into is due to the content body being formatted as a JSON (jsonObj.toString()) instead of form encoded. I would try one of two things:

Option 1: Change the Content-Type header to application/json. (I think this should work, if not try Option 2)

Option 2: Form-encode the request body instead of sending a JSON string. See this example.

Hopefully one of those works! Let us know if you still have trouble.

2 Likes

@stephen @shreya,

I took a few days off of development and returned to what seems like a timeout error when generating an Auth Token for my application.

I am using .Net to make the following call:

    var dwollaClient = DwollaClient.Create(true);
    var tokenRes = await dwollaClient.PostAuthAsync<TokenResponse>(
        new Uri($"{dwollaClient.AuthBaseAddress}/token"),
        new AppTokenRequest { Key = $"{dwollaApiKey}", Secret = $"{dwollaApiSecret }" }
    );

I am getting a response with empty Content, an ErrorCode of HttpClientException and and ErrorMessage of The operation was cancelled.

I have not made any changes to my AuthToken request for several weeks, can you check to make sure my Sandbox account is still operational?

If so, can you help identify the problem/solution here?

Hi @thomascjenks , this looks to be an error with the underlying HTTP client. Possibly the request isn’t making it to Dwolla because of a network timeout? Were you able to retry this request with any luck?

Thanks for getting back, and yes, it turned out to be a local network problem. A restart of my local machine seemed to resolve the problem!

1 Like