Get 403 error when fetch token

I tried to get token with my client_id and client_secret using this api https://api-sandbox.dwolla.com/token.
When I use postman for this api, I get a correct response like this.

{
  "access_token": "S4w8q1tr22l0eEKlFXHEFUFkmQlxdpwFmC3Zh9g6wtxQTFdxfs",
  "token_type": "Bearer",
  "expires_in": 3531
}

However, I want to fetch token using javascript fetch function on my frontend.
My code is bellow

     let headersList = {
      "Content-Type": "application/x-www-form-urlencoded",
      "User-Agent": "dwolla-v2-node 3.4.0"
     }
     
     let bodyContent = "client_id=HdnLpG7IFkUcICkUJkuvtdPJXj3RkfSDxkbOMRoOo8e3IZFMbJ&client_secret=1R7kh5dUGwhUFEfGpleG0Bk78KN80yjbDaeab8e52XZKNFbZly&grant_type=client_credentials";
     
     let response = await fetch("https://api-sandbox.dwolla.com/token", { 
       method: "POST",
       body: bodyContent,
       headers: headersList
     });

Here, I got 403 error for this request.

I am using sandbox environment.

I hope I can get the right answer for this issue asap

Hi @Milos_Marinkovic, it looks like you are using our dwolla-v2-node SDK. In that case, you can instantiate a “Client” and start calling the API using that client like so:

const Client = require("dwolla-v2").Client;

const dwolla = new Client({
    environment: "sandbox", // Defaults to "production"
    key: process.env.DWOLLA_APP_KEY,
    secret: process.env.DWOLLA_APP_SECRET
})
// POST https://api.dwolla.com/customers body={ ... }
const response = await dwolla.post("customers", {
    firstName: "Jane",
    lastName: "Doe",
    email: "jane.doe@example.com"
});

console.log("Created Resource: ", response.headers.get("Location"));

If you would like to continue using fetch, make sure that the client ID and secret are passed in as base64-encoded string client_id:client_secret to the Authorizatione header like so:

Authorization: Basic Base64(client_id:client_secret)

Hope this helps! Let us know if you have any questions!

This topic was automatically closed after 365 days. New replies are no longer allowed.