Trying to get an Access Token from Dwolla restful api

417 views Asked by At

Im having trouble getting an access token for the sandbox environment. Im following this guide for authenticating: OAuth

So when i create my request, following this guide, i get the following response from the api: {"error":"access_denied","error_description":"Invalid application credentials."}

Im using key for mf client ID and secret as my client secret as per the instructions.

Here is the code Im using:

public static void main(String[] args) {
    try {

        URL url = new URL("https://www.dwolla.com/oauth/v2/token");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");

            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("client_id", "<Key>");
            conn.setRequestProperty("client_secret", "<Secret>");
            conn.setRequestProperty("grant_type", "client_credentials"); 


            conn.setDoInput(true);
            conn.setDoOutput(true);



            System.out.println("Message:" + conn.getResponseMessage());

            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();
    } catch (MalformedURLException ex) {
        Logger.getLogger(PaymentTest.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(PaymentTest.class.getName()).log(Level.SEVERE, null, ex);
    }

}
1

There are 1 answers

0
user3712476 On

I was able to get an access token finally. My problem is first of all the above code uses client_id and client_secret as header params. These need to go in the body of the request.

My second problem is that I used the wrong content type for the message I was sending.

Here is the code that worked for me:

URL url = new URL("https://sandbox.dwolla.com/oauth/v2/token");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");

        conn.setRequestProperty("Content-Type", "application/json");


        conn.setDoInput(true);
        conn.setDoOutput(true);

        String data = "";


    JSONObject jsonObj = new JSONObject();
    jsonObj.put("client_id", "<Your Client ID>");
    jsonObj.put("client_secret", "<Your Client Secret>");
    jsonObj.put("grant_type", "client_credentials");

    data = jsonObj.toString();

    System.out.println("data = " + data);



    byte[] outputInBytes = data.getBytes("UTF-8");
    OutputStream os = conn.getOutputStream();
    os.write( outputInBytes );    
    os.close();


    System.out.println("Message:" + conn.getResponseMessage());


    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();