![](/img/trans.png)
[英]Calling Graph API inside a javascript Azure Function
[英]Calling Microsoft Graph API from inside Azure Functions
我正在尝试编写一个调用Microsoft Graph API的简单Azure函数。 但我无法使access_token工作。 这就是我所做的:
npm install request
) 而实际功能:
var request = require('request'); module.exports = function (context, req) { var token = req.headers['x-ms-token-aad-access-token']; var reqUrl = 'https://graph.microsoft.com/v1.0/me/'; request.get(reqUrl, {'auth': {'bearer': token}}, function (err, response, msg) { context.res = { body: msg }; context.done(); }); };
在单独的浏览器窗口中测试此功能。 我正确地签了AAD。
但是Graph返回的消息是:
"{ "error": { "code": "InvalidAuthenticationToken", "message": "CompactToken parsing failed with error code: -2147184105", "innerError": { "request-id": "4c78551d-f0fe-4104-b1d3-e2d96fd3c02c", "date": "2017-05-16T19:11:14" } } }"
我查看了从req.headers['x-ms-token-aad-access-token']
。 它类似于“AQABAA ....”,它似乎与我之前看到的以“eyJ ......”开头的常规access_token不同。
这可能有什么问题? 在调用Graph API时,我是否应该使用请求标头中的access_token?
谢谢!
根据Chris Gillum的建议,我也研究了“代表”流程。 这是我更新的函数,它通过提供id_token(从请求头中检索)获取特定资源的access_token(在我的情况下为https://graph.microsoft.com ):
var request = require('request');
module.exports = function (context, req) {
var parameters = {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
client_id: process.env.WEBSITE_AUTH_CLIENT_ID,
client_secret: process.env.WEBSITE_AUTH_CLIENT_SECRET,
assertion: req.headers['x-ms-token-aad-id-token'],
resource: 'https://graph.microsoft.com',
requested_token_use: 'on_behalf_of'
};
request.post('https://login.microsoftonline.com/microsoft.com/oauth2/token', {form: parameters}, function (aadErr, aadResponse, aadMsg) {
var msgJson = JSON.parse(aadMsg);
request.get('https://graph.microsoft.com/v1.0/me/', {'auth': {'bearer': msgJson.access_token}}, function (err, response, msg) {
context.res = {
body: msg
};
context.done();
});
});
};
使用Azure App Service身份验证/授权时,有两种方法可以使这项工作:
x-ms-token-aad-id-token
)以获取MS Graph访问令牌。 不需要任何代码更改的最简单方法是执行#1。 我在App Service Auth和Azure AD Graph API博客文章(需要一些更新)中概述了该过程,但我将在此处为Microsoft Graph提供功能优化版本。
您需要做的主要事情是:
"additionalLoginParams"
从null
更改为["resource=https://graph.microsoft.com"]
,并保存更改。 执行此操作并再次登录后, x-ms-token-aad-access-token
请求标头将始终为您提供与Microsoft Graph一起使用的访问令牌。
上述方法的缺点是,如果您需要从功能应用程序访问多个受AAD保护的资源,它将无法帮助您。 如果这对您来说是个问题,那么您需要使用上面的方法#2。
标题应包含正确的访问令牌(此处有更多详细信息): https : //docs.microsoft.com/en-us/azure/app-service-api/app-service-api-authentication
这是另一篇文章,它遇到了同样的错误,可能会有所帮助: 如何使用新的microsoft graph api创建一个auth令牌?
一种可能的解决方法是使用服务主体身份验证流,您可以在其中启用功能应用程序以通过AAD调用Graph API。
Azure Functions现在支持Microsoft Graph的本机身份验证。 文档位于https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-microsoft-graph
例如,您可以创建一个HttpTrigger函数并将以下内容添加到function.json中。
{
"type": "token",
"direction": "in",
"name": "graphToken",
"resource": "https://graph.microsoft.com",
"identity": "userFromRequest"
}
然后,您可以代表发出请求的用户查询Graph API。 访问令牌作为参数传入,您可以将其作为标头添加到HttpClient
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, string graphToken, TraceWriter log)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", graphToken);
return await client.GetAsync("https://graph.microsoft.com/v1.0/me/");
}
您还可以使用ClientCredentials身份验证模式运行功能,这意味着它作为应用程序运行,而不是在特定用户的上下文中运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.