Yesterday, I attempted to access Docusign's API in order to authenticate a user and obtain an access token. However, when trying to fetch the access token as outlined here, I encountered an "invalid_rant" error.
I successfully obtained the authorization code through the client, and below is my server-side code where I receive the code from the query parameters to request the access token. Despite attempting to use JSON.stringify for the body in the fetch call, it did not work as expected.
import { Controller, Get, Query } from '@nestjs/common';
import { json } from 'stream/consumers';
@Controller('docusign')
export class DocusignController {
@Get("access_token")
async getAccessToken(@Query("code") code:string) {
const auth_header = "Basic "+btoa(process.env.Docusing_Integration_Key+":"+process.env.Docusign_Auth_Secret);
let res= await fetch('https://account-d.docusign.com/oauth/token',{
method: 'POST',
headers: {
"Authorization":auth_header
},
body:`grant_type=authorization_code&code=${code}`
})
console.log(await res.json())
}
}
Upon running this code, I encountered an error which can be seen here.
However, when I replicated the same process in Postman (ensuring data consistency with console logs), I was able to retrieve the access token as intended, as shown here.