使用相同的API访问同一环境中的Web API。具体而言,需要确保使用的是相同的scopes和redirectUri,并确保在应用程序注册页面上为相同的应用程序定义了正确的回调URI。以下是代码示例:
var msalConfig = {
auth: {
clientId: 'your_client_id',
redirectUri: 'https://localhost:3000/auth.html',
authority: 'https://login.microsoftonline.com/common',
validateAuthority: true,
navigateToLoginRequestUrl: false,
},
cache: {
cacheLocation: 'localStorage',
storeAuthStateInCookie: false,
},
}
// Create the main MSAL instance
var msalInstance = new msal.PublicClientApplication(msalConfig);
// Login using the same scopes and redirectUri for all environments
msalInstance.loginPopup({ scopes: ['user.read'], redirectUri: 'https://localhost:3000/auth.html' })
.then(function (response) {
// Use the same access token for all environments
var token = response.accessToken;
console.log(token);
// Call the API using the same scopes and access token for all environments
callApi('https://your-web-api.com/api/data', token, ['user.read'])
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
})
.catch(function (error) {
console.log(error);
});
下一篇:不同环境下特殊字符无法正常显示