访问令牌未通过 Salesforce 的 Passport Strategy 刷新

问题描述 投票:0回答:1

我的任务的主要目的是使用有效的访问令牌在管道中运行 Salesforce API 测试,无需任何手动干预来再次生成它。

为了实现这一目标,我尝试使用 Passport 策略刷新访问令牌,然后在以下 API 调用中使用新令牌。以下代码运行时没有错误,但 API 调用没有返回任何响应,这让我认为 API 调用未运行。我无法在这里指出可能出现的问题,请分享您针对类似问题的方法。

const passport = require('passport');
const OAuth2Strategy = require('passport-oauth2').Strategy;
const refresh = require('passport-oauth2-refresh');
const fetch = require('node-fetch');

const strategy = new OAuth2Strategy({
    authorizationURL: 'https://test.salesforce.com/services/oauth2/authorize',
    tokenURL: 'https://test.salesforce.com/services/oauth2/token',
    clientID: 'xxxx',
    clientSecret: 'xxxx',
    callbackURL: 'https://test.salesforce.com/services/oauth2/callback' 
  },
  function(accessToken, refreshToken, profile, done) {
    // Handle authentication, e.g., save user information to session
    done(null, { accessToken, refreshToken, profile });
});

passport.use(strategy);
refresh.use(strategy);

describe('Salesforce API Test', () => {
    it('should make an authenticated API GET call to Salesforce connected app', async () => {
        const apiUrl = 'https://xxx.sandbox.my.salesforce.com/services/data/v60.0/sobjects/';

        // Get the access token from the passport user object
        const user = { accessToken: null, refreshToken: null };
        passport.authenticate('oauth2', (err, result) => {
            if (err) {
                throw new Error('Failed to authenticate with Salesforce');
            }
            user.refreshToken = result.refreshToken;
            user.accessToken = result.accessToken;
            console.log(user.accessToken); 

            const response = fetch(apiUrl, {
                method: "GET",
                headers: {
                    'Authorization': 'Bearer '+ user.accessToken,
                    'Content-type': 'application/json'
                },
            });
            const responseData = response.json();
            console.log(responseData);
            console.log(response.status);
       
        })
        
    });
});
salesforce passport.js refresh-token
1个回答
0
投票
  1. 检查您连接的应用程序是否支持客户端凭据流,并检查用户访问权限是否与您使用客户端凭据流生成访问令牌相同。
  2. 如果您使用 OAuth2.0 令牌代码流,请检查刷新访问令牌 API 是否已添加到您连接的应用程序中,如果没有连接该刷新令牌方法,然后将端点更改为刷新令牌端点并使用刷新您第一次验证时获得的令牌。
© www.soinside.com 2019 - 2024. All rights reserved.