【问题标题】:Salesforce - unsupported_grant_type: grant type not supportedSalesforce - unsupported_grant_type:不支持授权类型
【发布时间】:2022-02-02 01:13:28
【问题描述】:

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

这是我的登录代码:

@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,
      }),
    );
  }

这是我的回调代码:

@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);
    }
  }

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

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

【问题讨论】:

    标签: typescript oauth-2.0 salesforce nestjs jsforce


    【解决方案1】:

    错误是我将 loginUrl 设置为

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

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

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

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

    【讨论】:

    • 我遇到了同样的问题,我尝试了给定的解决方案,但它仍然抛出同样的错误,还有其他解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 2019-05-21
    • 2017-05-14
    • 2017-09-18
    • 1970-01-01
    相关资源
    最近更新 更多