Azure Functions Timer with Graph Api



我有一个使用图形API的计时器函数(beta版本/v2(。 但我收到以下错误。

我现在收到此错误:

2018-10-10T08:52:34.019 [错误] Microsoft.Azure.WebJobs.Host:错误索引方法"Functions.TimerTriggerMeetingRoom"。Microsoft.Azure.WebJobs.Host:无法解析绑定参数"标头"。绑定表达式必须映射到触发器提供的值或触发器绑定到的值的属性,或者必须是系统绑定表达式(例如.sys.randguid、sys.utcnow 等(。

这是我的"run.csx"代码:

#r "Newtonsoft.Json"
#r "System.Configuration"
#r "System.Data"

using System.Net; 
using System.Net.Http; 
using System.Net.Http.Headers; 
using System.Text;
using System;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Configuration;
using System.Threading.Tasks;
public static async Task Run(TimerInfo myTimer, string graphToken, ILogger log)
{
var currentTime = DateTime.UtcNow;
var ramsey = new List<Ramsey>();
log.LogInformation("C# HTTP trigger function processed a request.");
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", graphToken);
var json = await client.GetStringAsync("https://graph.microsoft.com/v1.0/me/");

log.LogInformation("C# HTTP trigger function processed a request.");
JObject jResults = JObject.Parse(json);
//log.LogInformation(jResults.ToString());
var result= jResults["value"];
log.LogInformation("value: "+result.ToString());
return new HttpResponseMessage(HttpStatusCode.OK) 
{
Content = new StringContent(json, Encoding.UTF8, "application/json") 
};
}

public class Ramsey
{
public string subject { get; set; }
}

"Function.json":

{
"bindings": [
{
"name": "myTimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 */15 * * * *"
},
{
"name": "$return",
"type": "http",
"direction": "out"
},
{
"type": "token",
"name": "graphToken",
"resource": "https://graph.microsoft.com",
"identity": "UserFromRequest",
"direction": "in"
}
]
}

这是我的"functions.proj"文件:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Mobile.Client" Version="2.0.0"/>
<PackageReference Include="MySql.Data" Version="7.0.7-m61"/>
</ItemGroup>
</Project>

请让我知道。提前谢谢你

这里的关键问题是使用带有计时器触发器的"identity": "UserFromRequest"。该标识模式仅受 HttpTrigger 支持,因为它从 HTTP 请求中提取请求信息。

对于计时器驱动的方案,标识的两个最佳选项是clientCredentialsuserFromId。 在这种情况下,clientCredentials可能不是你想要的,因为你正在访问 Graph 资源,并且函数应用程序的服务主体在图中可能没有任何有趣的内容。

要使用userFromId身份模式,您需要执行以下操作:

  1. 通过向function.json添加"userId":"<principalId>"来指定用户的主体 ID
  2. 确保上面指定的用户已登录到函数应用程序。这可以通过让用户在https://<function-app-name>.azurewebsites.net/.auth/login/aad直接登录自己或让客户端使用以下正文向上述 URL 发出 POST 请求来实现(注意:所有令牌都有函数应用程序的受众(:

{ "id_token": "<idToken>", "access_token": "<accessToken>", "refresh_token": "<refreshToken>" }

最新更新