Azure active directory 使用托管服务标识调用SharePoint Online。可能的

Azure active directory 使用托管服务标识调用SharePoint Online。可能的,azure-active-directory,sharepoint-online,azure-logic-apps,Azure Active Directory,Sharepoint Online,Azure Logic Apps,我一直在Azure逻辑应用程序和Azure功能应用程序中使用托管服务标识。我认为这是自切片面包以来最好的事情,我正在尝试启用各种场景,其中之一是使用MSI获取应用程序专用令牌并调用SharePoint Online 使用Logic Apps,我为我的应用程序生成了托管服务标识,并在SharePoint应用程序上授予它Sites.readwrite.All。当使用HTTP操作时,我能够调用REST端点,同时使用托管服务标识作为身份验证并使用https://.sharepoint.com 作为观众

我一直在Azure逻辑应用程序和Azure功能应用程序中使用托管服务标识。我认为这是自切片面包以来最好的事情,我正在尝试启用各种场景,其中之一是使用MSI获取应用程序专用令牌并调用SharePoint Online

使用Logic Apps,我为我的应用程序生成了托管服务标识,并在SharePoint应用程序上授予它Sites.readwrite.All。当使用HTTP操作时,我能够调用REST端点,同时使用托管服务标识作为身份验证并使用https://.sharepoint.com 作为观众

然后我想我会更进一步,创建一个功能应用程序,并遵循相同的模式。我创建了应用程序,生成了MSI,并将其添加到Sites.readwrite.All角色中,与使用Logic应用程序的方式相同

然后,我使用下面的代码检索访问令牌并尝试生成clientcontext:

#r "Newtonsoft.Json"
using Newtonsoft.Json;
using System;
using System.Net; 
using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.SharePoint.Client;
public static void Run(string input, TraceWriter log)
{
    string resource = "https://<tenant>.sharepoint.com";
    string apiversion = "2017-09-01";
    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Add("Secret", Environment.GetEnvironmentVariable("MSI_SECRET"));
        var response = client.GetAsync(String.Format("{0}/?resource={1}&api-version={2}", Environment.GetEnvironmentVariable("MSI_ENDPOINT"), resource, apiversion)).Result;
            var responseContent = response.Content; 
            string responseString = responseContent.ReadAsStringAsync().Result.ToString();
            var json = JsonConvert.DeserializeObject<dynamic>(responseString);
            string accesstoken = json.access_token.ToString()
            ClientContext ctx = new ClientContext("<siteurl>");
            ctx.AuthenticationMode = ClientAuthenticationMode.Anonymous;
            ctx.FormDigestHandlingEnabled = false;
            ctx.ExecutingWebRequest += delegate (object sender, WebRequestEventArgs e){ 
                e.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + accesstoken;
            };
            Web web = ctx.Web;
            ctx.Load(web);
            ctx.ExecuteQuery();
            log.Info(web.Id.ToString());
    }
}
#r“Newtonsoft.Json”
使用Newtonsoft.Json;
使用制度;
Net系统;
使用System.Net.Http;
使用System.Net.Http.Header;
使用Microsoft.SharePoint.Client;
公共静态无效运行(字符串输入、TraceWriter日志)
{
字符串资源=”https://.sharepoint.com";
字符串apiversion=“2017-09-01”;
使用(var client=new HttpClient())
{
client.DefaultRequestHeaders.Add(“Secret”、Environment.GetEnvironmentVariable(“MSI_Secret”);
var response=client.GetAsync(String.Format({0}/?resource={1}&api version={2}),Environment.GetEnvironmentVariable(“MSI_端点”),resource,apiversion)).Result;
var responseContent=response.Content;
string responseString=responseContent.ReadAsStringAsync().Result.ToString();
var json=JsonConvert.DeserializeObject(responseString);
字符串accesstoken=json.access\u token.ToString()
ClientContext ctx=新的ClientContext(“”);
ctx.AuthenticationMode=ClientAuthenticationMode.Anonymous;
ctx.FormDigestHandlingEnabled=false;
ctx.ExecutingWebRequest+=委托(对象发送者,WebRequestEventArgs e){
e、 WebRequestExecutor.RequestHeaders[“授权”]=“承载者”+访问令牌;
};
Web=ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
log.Info(web.Id.ToString());
}
}
已生成承载令牌,但请求失败,401访问被拒绝(reason=“验证请求时出现错误”。“category=“invalid\u client”)

我已尝试将访问群体更改为0000000 3-0000-0ff1-ce00-000000000000/.sharepoint.com@“但这会产生不同的401错误,基本上表示它无法验证访问群体uri。(“错误描述”:“引发了类型为“Microsoft.IdentityModel.Tokens.AudienceUrivalizationFailedException”的异常。)。我还将CSOM调用替换为REST调用,模拟我使用Logic应用程序进行的相同调用

我对OAuth2的理解还不足以理解我为什么会遇到问题以及下一步要看什么

为什么使用HTTP操作的逻辑应用程序调用工作,为什么函数应用程序不工作


有人吗?

非常基本的问题:您是否尝试过在资源字符串的末尾添加“/”,例如


此问题也存在于其他端点中,因此它可能也会干扰此处。

谢谢提示!我回家后会试试的,告诉你就是这样!谢谢你的帮助!