Azure active directory 在8中使用MSAL库获取Microsoft Azure广告组名称列表
我正在开发Angular 8应用程序,该应用程序试图检索登录用户可用的所有Microsoft广告组的列表。 之前,我正在使用MSAL库对用户进行身份验证,并成功获得以下值:Azure active directory 在8中使用MSAL库获取Microsoft Azure广告组名称列表,azure-active-directory,microsoft-graph-api,angular8,msal,usergroups,Azure Active Directory,Microsoft Graph Api,Angular8,Msal,Usergroups,我正在开发Angular 8应用程序,该应用程序试图检索登录用户可用的所有Microsoft广告组的列表。 之前,我正在使用MSAL库对用户进行身份验证,并成功获得以下值: 访问令牌 idToken 家庭标识符 呼气素 我的MSAL配置如下所示: export function MSALAngularConfigFactory(): MsalAngularConfiguration { var isIE = window.navigator.userAgent.indexOf("
- 访问令牌
- idToken
- 家庭标识符
- 呼气素
export function MSALAngularConfigFactory(): MsalAngularConfiguration {
var isIE = window.navigator.userAgent.indexOf("MSIE ") > -1 ||
window.navigator.userAgent.indexOf("Trident/") > -1;
return {
popUp: !isIE,
consentScopes: [
'user.read',
'openid',
'profile',
'group.Read.All'
],
unprotectedResources: ['https://www.microsoft.com/en-us/'],
protectedResourceMap: [
['https://graph.microsoft.com/v1.0/me', ['user.read']],
['https://graph.microsoft.com/v1.0/groups', ['group.read.all']]
]
};
}
当我调用MSAL的getAccount().idTokenClaims['groups']方法时,它会返回组数组,其中只包含一些标识符值,这不是我的要求。我需要一个广告组名称数组
我还实现了MicrosoftGraph库,用于使用以下API获取组:
fetch('https://graph.microsoft.com/v1.0/groups', {
headers: {
'Authorization': 'Bearer ' + {access_token},
'Content-Type': 'application/json',
}
}).then(res => { console.log(res); });
上述代码总是导致错误,说明:
401(未经授权)
我尝试了Microsoft Graph的多种解决方案,包括:
const msalConfig = {
auth: {
clientId: clientId, // Client Id of the registered application
redirectUri: "https://localhost:4200",
}
};
const graphScopes = ["user.read", "group.read.all"]; // An array of graph scopes
const msalApplication = new UserAgentApplication(msalConfig);
const options = new MSALAuthenticationProviderOptions(graphScopes);
const authProvider = new ImplicitMSALAuthenticationProvider(msalApplication, options);
this.subscription1 = broadcast.subscribe("msal:loginSuccess",
(result) => {
//The result has accessToken as null.
fetch('https://graph.microsoft.com/v1.0/groups', {
headers: {
'Authorization': 'Bearer ' + {accessToken},
'Content-Type': 'application/json',
}
}).then(response => { console.log(response);});
上述代码段也返回一个错误,说明:
CompactToken解析失败,错误代码为80049217
我尝试了更多的解决方案,但都不管用。我从昨天起就被困在里面了
有人能帮我找出最好的解决办法吗。我们将非常感谢您的帮助
提前感谢。不太清楚应用程序的上下文。我的代码演示基于 在
src/app/app.module.ts
正确配置此应用程序后,请转到src/profile/profile.component.ts
添加以下函数以获取所有组名:
getAccessTokenAndCallGraphAPI(){
this.authService.acquireTokenSilent({
scopes: ['group.Read.All']
}).then(result=>{
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
Authorization: 'Bearer '+result.accessToken
})}
this.http.get("https://graph.microsoft.com/v1.0/groups?$select=id,displayName",httpOptions).toPromise().then(result=>{console.log(result)});
})
}
在page init上调用此函数:
ngOnInit() {
this.getProfile();
this.getAccessTokenAndCallGraphAPI();
}
结果:
访问“配置文件”页面将在控制台中打印组的所有ID和名称。
在调用此API之前,请确保您已授予应用程序以下权限:
看来主要问题是访问令牌无效。你是怎么得到的?把它解码,你就能看到它的细节。嗨@AllenWu。谢谢你的观察。这实际上是我的accessToken的问题。我使用的方法没有返回任何accessToken。我使用的另一种方法是处理过期的令牌。io是一种非常简单和优雅的方式来检查您的令牌状态。非常感谢您的回复。它就像一个符咒。我试图创建一个userApplicationAgent实例,AuthenticationProvider作为参数传递给acquireTokenSilent,查看一些文档并调用fetch方法以使用graph获取数据组。但这会导致accessToken为空。但这段代码工作得完美无缺。我还有一个类似的问题。我过一会儿也会想到的。我相信我也会得到详细和正确的解释。再次感谢。
ngOnInit() {
this.getProfile();
this.getAccessTokenAndCallGraphAPI();
}