Salesforce - unsupported_grant_type: grant type not supported

17.3k Views Asked by At

I have an issue when connecting to my Salesforce account using JSforce and OAuth2 in my NestJS (Typescript) application.

Here is my login code:

@Get('salesforce/login/:userId')
  async login(@Req() req, @Res() res, @Param('userId') userId) {
    const user = await this.userService.findById(userId);
    const oauth2 = new OAuth2({
      loginUrl: user.salesforceOauthInformation.loginUrl,
      clientId: user.salesforceOauthInformation.clientId,
      clientSecret: user.salesforceOauthInformation.clientSecret,
      redirectUri: user.configService.salesforceRedirectUri,
    });
    res.redirect(
      oauth2.getAuthorizationUrl({
        scope: 'api full id web refresh_token',
        state: user.id,
      }),
    );
  }

Here is my callback code:

@Get('salesforce/callback')
  async callback(@Query('code') code: string, @Query('state') state: string) {
    const user = await this.userService.findById(state);
    const oauth2 = new OAuth2({
      loginUrl: user.salesforceOauthInformation.loginUrl,
      clientId: user.salesforceOauthInformation.clientId,
      clientSecret: user.salesforceOauthInformation.clientSecret,
      redirectUri: 'http://localhost:4000/crm/auth/salesforce/callback',
    });
    const connection = new Connection({
      oauth2,
    });
    try {
      await connection.authorize(code);
      await this.userService.update(user.id, {
        salesforceOauthInformation: {
          loginUrl: connection.oauth2.loginUrl,
          clientId: connection.oauth2.clientId,
          clientSecret: connection.oauth2.clientSecret,
          accessToken: connection.accessToken,
          refreshToken: connection.refreshToken,
        },
      });
    } catch (e) {
      this.logger.error(e);
    }
  }

Here is the error throwing on await connection.authorize(code);:

error: 17:44:50.228+01:00 [CrmService] unsupported_grant_type: grant type not supported
3

There are 3 best solutions below

1
Hugo Sohm On BEST ANSWER

The error is that I was setting my loginUrl as

https://my-company.lightning.force.com

But the loginUrl must be set as my.salesforce.com domain instead of lightning.force.com

https://my-company.my.salesforce.com

Solution found on this website: https://www.javaniceday.com/post/sfdx-error-authenticating-with-auth-code-due-to-grant-type-not-supported

0
Bruno Degomme On

I had this issue when trying to test the Oauth flow of my connected app. There were two reasons

  • needed to add "Perform request at any time (refresh_token, online access)" scope
  • needed to use https://login.salesforce.com/services/oauth2/authorize as base url for the authorization
0
Dharmendrasinh Chudasama On

I hope this may help you, as it is working for me

  • give values this way, and once it will give proper response click on Code button on right side below SAVE button
  • Possible reasons of issue: Need given header, issue in properly encode of values

Postman screenshot