调用 Web API 的 Web 应用:调用 Web API

现在你已有令牌,可以调用受保护的 Web API 了。 通常从 Web 应用的控制器或页面调用下游 API。

调用受保护的 Web API

调用受保护的 Web API 取决于所选的语言和框架:

成功检索令牌后,代码使用 axios 包来查询 API 终结点并检索 JSON 结果。

/**
 * Attaches a given access token to a MS Graph API call
 * @param endpoint: REST API endpoint to call
 * @param accessToken: raw access token string
 */
async function fetch(endpoint, accessToken) {
    const options = {
        headers: {
            Authorization: `Bearer ${accessToken}`
        }
    };

    console.log(`request made to ${endpoint} at: ` + new Date().toString());

    try {
        const response = await axios.get(endpoint, options);
        return await response.data;
    } catch (error) {
        throw new Error(error);
    }
}

后续步骤

  • 通过在以下多部分教程系列中生成可用户登录的 ASP.NET Core Web 应用来了解详细信息

  • 浏览 Microsoft 标识平台 Web 应用示例