oauth-2.0 Salesforce -不支持的授权类型:不支持授权类型

bxpogfeg  于 2022-10-31  发布在  其他
关注(0)|答案(2)|浏览(305)

在我的NestJSTypescript)应用程序中使用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,
      }),
    );
  }

下面是我的回调代码:

@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
lmvvr0a8

lmvvr0a81#

错误是我将我的loginUrl设置为

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

但是,登录URL必须设置为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

nc1teljy

nc1teljy2#

我在尝试测试连接的应用程序的Oauth流时遇到了这个问题。

  • 需要添加“随时执行请求(refresh_token,online access)”范围
  • 需要使用https://login.salesforce.com/services/oauth2/authorize作为授权的基本url

相关问题