typescriptoauth-2.0nestjsdocusignapiaccess-token

Fetching Docusign Returns Invalid Grant In Code But Works Fine In Postman


Yesterday I tried to fetch docusigns api in order to validate a user and get an access token. However when I tried fetching to get the access token as is described here, I get an invalid_rant error. https://developers.docusign.com/platform/auth/authcode/authcode-get-token/

I manage getting the authorization code via the client, and this is my server side code where I recive the code from the query parameters in order to request the access token. In the fetch I also tried using JSON.stringify for the body to no avail.

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);
        //Cambiar a este url para produccion https://account.docusign.com/oauth/token
       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())
        
    }

   

}

When this runs I get the following error enter image description here

But when I go to postman and input the same data (I console logged the data to ensure it was the same) I get the access token as expected enter image description here


Solution

  • I apologize yesterday I wasn't able to solve it, but today I realized I was only missing to add the content-type to the headers.

        @Get("access_token")
    async getAccessToken(@Query("code") code:string) {
    
        const auth_header = "Basic "+btoa(process.env.Docusing_Integration_Key+":"+process.env.Docusign_Auth_Secret);
        console.log(auth_header)
        console.log(code)
        //Cambiar a este url para produccion https://account.docusign.com/oauth/token
       let res= await fetch('https://account-d.docusign.com/oauth/token',{
            method: 'POST',
            headers: {
                "Authorization":auth_header,
                'Content-Type': 'application/x-www-form-urlencoded'//<-- add this line
            },
            body:`grant_type=authorization_code&code=${code}`
                  
        })
        console.log(await res.json())
        
    }