获取授权令牌以调用API [英] Get Authorization token to call API

查看:185
本文介绍了获取授权令牌以调用API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用由

I'm trying to call the API that I created by following these directions, I got to the point where I can call the API from Developer Portal using the JWT token.

现在我很困惑,Angular客户端应用程序将如何获取此JWT令牌以调用API?

Now I'm confused, how will the angular client app get this JWT token in order to call the API?

当前,Angular应用程序的用户位于Active Directory(用于设置API的指导中使用的同一AD)中.登录过程是通过 MSAL库完成的.当我尝试通过调用

Currently, users of the angular app are in the Active Directory (same AD that was used in directions to set up the API). The sign-in process is done through MSAL library. When I try to get the token by calling acquireTokenSilent and try to call the API using this token, I get 401 error.

如何从angular应用程序中获取正确的JTW令牌?

How do I get the correct JTW token from the angular app?

推荐答案

请确保使用api应用程序中公开的权限授予客户端应用程序的权限,请遵循以下

Make sure you grant permissions for your client app with the permissions you exposed in the api app, follow this doc.

然后在

Then in the consentScopes , use the api scope of your api, you can find the Application ID URL in the Expose an API page of your API App, e.g. something like api://xxxxxxxxxxxxxxx/api_usage

consentScopes: [
        '<Application ID URL>/scope'
      ],

获取令牌时,将["<Application ID URL>/scope"]用作scopes.

const requestObj = {
    scopes: ["<Application ID URL>/scope"]
};

this.authService.acquireTokenSilent(requestObj).then(function (tokenResponse) {
    // Callback code here
    console.log(tokenResponse.accessToken);
}).catch(function (error) {
    console.log(error);
});

有关更多详细信息,请参阅教程:登录用户并从Angular单页应用程序中调用Microsoft Graph API .在此文档中,它调用了MS Graph,可以调用您自己的api,更改范围,并且应该可以使用.

Fore more details, refer to Tutorial: Sign in users and call the Microsoft Graph API from an Angular single-page application. In this doc, it calls the MS Graph, to call your own api, change the scopes and it should work.

这篇关于获取授权令牌以调用API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆