Salesforce - unsupported_grant_type:不支持授权类型

Hug*_*ohm 0 salesforce oauth-2.0 typescript nestjs jsforce

我在NestJS ( Typescript ) 应用程序中使用JSforceOAuth2连接到我的Salesforce帐户时遇到问题。

这是我的登录代码:

@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,
      }),
    );
  }
Run Code Online (Sandbox Code Playgroud)

这是我的回调代码:

@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);
    }
  }
Run Code Online (Sandbox Code Playgroud)

这是抛出的错误await connection.authorize(code);

error: 17:44:50.228+01:00 [CrmService] unsupported_grant_type: grant type not supported
Run Code Online (Sandbox Code Playgroud)

Hug*_*ohm 6

错误是我将登录网址设置为

https://my-company.lightning.force.com
Run Code Online (Sandbox Code Playgroud)

loginUrl必须设置为my.salesforce.com域而不是lightning.force.com

https://my-company.my.salesforce.com
Run Code Online (Sandbox Code Playgroud)

在此网站上找到的解决方案: https: //www.javaniceday.com/post/sfdx-error-authentication-with-auth-code-due-to-grant-type-not-supported

  • 我遇到了同样的问题,我尝试了给定的解决方案,但它仍然抛出相同的错误,还有其他解决方案吗? (2认同)