我正在使用 NodeJs SDK 使用 Appid 实现,我目前正在尝试ApplicationIdentityToken
通过TokenManager
. 下面是我的代码片段。
给你一个有效的tokenManager.getApplicationIdentityToken()
令牌,但我面临的问题是,每当我将此令牌传递给userProfileManager.getUserInfo(token)
它时,它都会给我一个UnauthorizedException
.
我已经剥离了整个代码并创建了一个小函数来测试令牌的获取并使用该userProfileManager.getUserInfo
函数进行验证。
注意:请忽略仅用于提供代码片段的反模式。
const userProfileManager = require('ibmcloud-appid').UserProfileManager;
userProfileManager.init({
oauthServerUrl: process.env.APPID_URL,
profilesUrl: process.env.APPID_PROFILES_URL,
});
const config = {
tenantId: process.env.TENANT_ID,
clientId: process.env.CLIENT_ID,
secret: process.env.CLIENT_SECRET,
oauthServerUrl: process.env.APPID_URL,
profilesUrl: process.env.APPID_PROFILES_URL,
};
let token = '';
const { TokenManager } = require('ibmcloud-appid');
const tokenManager = new TokenManager(config);
const getAppIdentityToken = async () => {
tokenManager
.getApplicationIdentityToken()
.then((appIdAuthContext) => {
console.log(` Access tokens from SDK : ${JSON.stringify(appIdAuthContext)}`);
token = appIdAuthContext.accessToken;
})
.then(async () => {
const data = await userProfileManager.getUserInfo(token);
console.log(data);
})
.catch((err) => {
console.error(err);
});
};
exports.getAppIdentityToken = getAppIdentityToken;