[英]Azure Functions Timer with Graph Api
我有一個使用Graph Api的計時器函數(測試版/ 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請求中提取請求信息。
對於計時器驅動的方案,身份的兩個最佳選擇是clientCredentials
或userFromId
。 在這種情況下, clientCredentials
可能不是您想要的,因為您正在訪問Graph資源,並且函數應用程序的服務主體可能在圖中沒有任何有趣的內容。
要使用userFromId
身份模式,您需要執行以下操作:
function.json
添加"userId":"<principalId>"
,為用戶指定主體ID。 https://<function-app-name>.azurewebsites.net/.auth/login/aad
或讓客戶端對上述URL發出POST請求來實現。下面的正文(注意:所有令牌都有該功能應用程序的訪問者): { "id_token": "<idToken>", "access_token": "<accessToken>", "refresh_token": "<refreshToken>" }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.