Microsoft graph api 在Microsoft团队中使用MSAL不工作JS
我在一个连接到MS Graph的MS团队应用程序上工作。 我正在尝试为MS团队中的MS Graph获取访问令牌。要获取令牌,我使用MSAL js 如果我使用Microsoft graph api 在Microsoft团队中使用MSAL不工作JS,microsoft-graph-api,msal,microsoft-teams,msal.js,Microsoft Graph Api,Msal,Microsoft Teams,Msal.js,我在一个连接到MS Graph的MS团队应用程序上工作。 我正在尝试为MS团队中的MS Graph获取访问令牌。要获取令牌,我使用MSAL js 如果我使用gulp serve运行应用程序,我将收到一个有效令牌,并且我可以访问MS Graph端点。但是,如果我构建应用程序并在MS团队中安装,则永远不会执行函数userAgentApplication.acquireTokenSilent(config)。在调用之前和之后,我使用console.log对其进行了测试。没有抛出错误 你知道为什么微软团
gulp serve
运行应用程序,我将收到一个有效令牌,并且我可以访问MS Graph端点。但是,如果我构建应用程序并在MS团队中安装,则永远不会执行函数userAgentApplication.acquireTokenSilent(config)
。在调用之前和之后,我使用console.log对其进行了测试。没有抛出错误
你知道为什么微软团队(app和webapp)不执行上述代码段吗
新的: 主页:
export function login() {
const url = window.location.origin + '/login.html';
microsoftTeams.authentication.authenticate({
url: url,
width: 600,
height: 535,
successCallback: function(result: string) {
console.log('Login succeeded: ' + result);
let data = localStorage.getItem(result) || '';
localStorage.removeItem(result);
let tokenResult = JSON.parse(data);
storeToken(tokenResult.accessToken)
},
failureCallback: function(reason) {
console.log('Login failed: ' + reason);
}
});
}
登录时
microsoftTeams.initialize();
// Get the tab context, and use the information to navigate to Azure AD login page
microsoftTeams.getContext(function (context) {
// Generate random state string and store it, so we can verify it in the callback
let state = _guid();
localStorage.setItem("simple.state", state);
localStorage.removeItem("simple.error");
// See https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-protocols-implicit
// for documentation on these query parameters
let queryParams = {
client_id: "XXX",
response_type: "id_token token",
response_mode: "fragment",
scope: "https://graph.microsoft.com/User.Read openid",
redirect_uri: window.location.origin + "/login-end.html",
nonce: _guid(),
state: state,
login_hint: context.loginHint,
};
// Go to the AzureAD authorization endpoint (tenant-specific endpoint, not "common")
// For guest users, we want an access token for the tenant we are currently in, not the home tenant of the guest.
let authorizeEndpoint = `https://login.microsoftonline.com/${context.tid}/oauth2/v2.0/authorize?` + toQueryString(queryParams);
window.location.assign(authorizeEndpoint);
});
// Build query string from map of query parameter
function toQueryString(queryParams) {
let encodedQueryParams = [];
for (let key in queryParams) {
encodedQueryParams.push(key + "=" + encodeURIComponent(queryParams[key]));
}
return encodedQueryParams.join("&");
}
// Converts decimal to hex equivalent
// (From ADAL.js: https://github.com/AzureAD/azure-activedirectory-library-for-js/blob/dev/lib/adal.js)
function _decimalToHex(number) {
var hex = number.toString(16);
while (hex.length < 2) {
hex = '0' + hex;
}
return hex;
}
// Generates RFC4122 version 4 guid (128 bits)
// (From ADAL.js: https://github.com/AzureAD/azure-activedirectory-library-for-js/blob/dev/lib/adal.js)
function _guid() {...}
虽然我的回答很晚,但我最近也遇到了同样的问题 解决方案非常简单:MSAL静默登录在MSTeam中不起作用,因为MSTeam依赖于MSAL不支持的IFrame方法 你可以在书中读到这一切 幸运的是,他们即将在msal 1.2.0版中发布此版本的修复程序,并且已经有了一个npm可安装测试版,该测试版应该可以实现这一点:
npm安装msal@1.2.0-beta.2
更新:我自己也尝试过,但测试版也不起作用。这一点在我自己的文章中得到了微软的证实
因此,我想目前,您不能为MS团队使用MSAL。您使用的是Bot还是Tab?这是有关的文档。这是一个很好的关于身份验证的视频。感谢您的回复新
MicrosoftTeam.authentication.authentication
工作正常,但仅在浏览器团队应用程序中。在本机应用程序中,登录弹出窗口作为标准浏览器选项卡打开,然后什么也不会发生@Wajeed MSFT能否请您尝试一次以查看身份验证流程体验?如果您遇到任何问题,请告诉我。另外,请参阅以下内容:,特别是步骤3。您是否注册了successCallback()函数,如果注册了,是否正在调用该函数?
microsoftTeams.initialize();
localStorage.removeItem("simple.error");
let hashParams = getHashParameters();
if (hashParams["error"]) {
// Authentication/authorization failed
localStorage.setItem("simple.error", JSON.stringify(hashParams));
microsoftTeams.authentication.notifyFailure(hashParams["error"]);
} else if (hashParams["access_token"]) {
// Get the stored state parameter and compare with incoming state
let expectedState = localStorage.getItem("simple.state");
if (expectedState !== hashParams["state"]) {
// State does not match, report error
localStorage.setItem("simple.error", JSON.stringify(hashParams));
microsoftTeams.authentication.notifyFailure("StateDoesNotMatch");
} else {
// Success -- return token information to the parent page.
// Use localStorage to avoid passing the token via notifySuccess; instead we send the item key.
let key = "simple.result";
localStorage.setItem(key, JSON.stringify({
idToken: hashParams["id_token"],
accessToken: hashParams["access_token"],
tokenType: hashParams["token_type"],
expiresIn: hashParams["expires_in"]
}));
microsoftTeams.authentication.notifySuccess(key);
}
} else {
// Unexpected condition: hash does not contain error or access_token parameter
localStorage.setItem("simple.error", JSON.stringify(hashParams));
microsoftTeams.authentication.notifyFailure("UnexpectedFailure");
}
// Parse hash parameters into key-value pairs
function getHashParameters() {
let hashParams = {};
location.hash.substr(1).split("&").forEach(function (item) {
let s = item.split("="),
k = s[0],
v = s[1] && decodeURIComponent(s[1]);
hashParams[k] = v;
});
return hashParams;
}