Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ';提供了无效的请求URI。请求URI必须是绝对URI,或者必须设置BaseAddress';连接到AF中的SP时出现异常_C#_Office365_Azure Functions_Csom - Fatal编程技术网

C# ';提供了无效的请求URI。请求URI必须是绝对URI,或者必须设置BaseAddress';连接到AF中的SP时出现异常

C# ';提供了无效的请求URI。请求URI必须是绝对URI,或者必须设置BaseAddress';连接到AF中的SP时出现异常,c#,office365,azure-functions,csom,C#,Office365,Azure Functions,Csom,我正在尝试连接Azure功能中的Sharepoint客户端(Sharepoint列表)。但是,当尝试连接时,无论我做什么,我都会收到异常: '提供了无效的请求URI。请求URI必须是绝对>URI或必须设置基地址' 在这里我粘贴代码,请告诉我是否有人可以检测到任何东西 string sharepointList = "https://someplace.sharepoint.com/teams/SomePlace/SomePlaceAppDev/SomeTeam/SomeRegion"; stri

我正在尝试连接Azure功能中的Sharepoint客户端(Sharepoint列表)。但是,当尝试连接时,无论我做什么,我都会收到异常:

'提供了无效的请求URI。请求URI必须是绝对>URI或必须设置基地址'

在这里我粘贴代码,请告诉我是否有人可以检测到任何东西

string sharepointList = "https://someplace.sharepoint.com/teams/SomePlace/SomePlaceAppDev/SomeTeam/SomeRegion";
string sourceListName = "Some Sites";

string accessToken = await getSharePointToken(log, sharepointList);
var collListItem = GetSharePointSiteList(sharepointList, sourceListName, accessToken);

public static ClientContext GetClientContext(string siteUrl, string accessToken)
{
    var ctx = new ClientContext(siteUrl);
    ctx.ExecutingWebRequest += (s, e) =>
    {
        e.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + accessToken;
    };
    return ctx;
}

public static async Task<string> getSharePointToken(TraceWriter log, string siteUrl)
{
    string clientID = ConfigurationManager.AppSettings["clientID"];
    string clientSecret = ConfigurationManager.AppSettings["clientSecret"];
    string tenantURL = ConfigurationManager.AppSettings["tenantURL"];
    string tenantID = ConfigurationManager.AppSettings["tenantID"];
    string spPrinciple = ConfigurationManager.AppSettings["spPrinciple"];
    string spAuthUrl = ConfigurationManager.AppSettings["spAuthUrl"];
    //public string TenantURL { get => tenantURL; }

    HttpClient client = new HttpClient();
    KeyValuePair<string, string>[] body = new KeyValuePair<string, string>[]
    {
        new KeyValuePair<string, string>("grant_type", "client_credentials"),
        new KeyValuePair<string, string>("client_id", $"{clientID}@{tenantID}"),
        new KeyValuePair<string, string>("resource", $"{spPrinciple}/{siteUrl}@{tenantID}".Replace("https://", "")),
        new KeyValuePair<string, string>("client_secret", clientSecret)
    };
    var content = new FormUrlEncodedContent(body);
    var contentLength = content.ToString().Length;
    string token = "";
    using (HttpResponseMessage response = await client.PostAsync(spAuthUrl, content))
    {
        if (response.Content != null)
        {
            string responseString = await response.Content.ReadAsStringAsync();
            JObject data = JObject.Parse(responseString);
            token = data.Value<string>("access_token");
        }
    }
    return token;
}

private static ListItemCollection GetSharePointSiteList(string sourceSiteURL, string sourceListName, string accessToken)
{
    ClientContext clientContext = GetClientContext(System.Net.WebUtility.UrlEncode(sourceSiteURL), accessToken);

    Microsoft.SharePoint.Client.List oList = clientContext.Web.Lists.GetByTitle(sourceListName);
    ListItemCollection collListItem = oList.GetItems(CamlQuery.CreateAllItemsQuery());

    clientContext.Load(collListItem);
    clientContext.ExecuteQuery();

    clientContext.Dispose();

    return collListItem;
}
字符串共享点列表=”https://someplace.sharepoint.com/teams/SomePlace/SomePlaceAppDev/SomeTeam/SomeRegion";
string sourceListName=“某些站点”;
string accessToken=Wait getSharePointToken(日志,sharepointList);
var collListItem=GetSharePointSiteList(sharepointList、sourceListName、accessToken);
公共静态ClientContext GetClientContext(字符串siteUrl、字符串accessToken)
{
var ctx=新的ClientContext(siteUrl);
ctx.ExecutingWebRequest+=(s,e)=>
{
e、 WebRequestExecutor.RequestHeaders[“授权”]=“承载者”+访问令牌;
};
返回ctx;
}
公共静态异步任务getSharePointToken(TraceWriter日志,字符串siteUrl)
{
字符串clientID=ConfigurationManager.AppSettings[“clientID”];
字符串clientSecret=ConfigurationManager.AppSettings[“clientSecret”];
字符串tenantURL=ConfigurationManager.AppSettings[“tenantURL”];
字符串tenantID=ConfigurationManager.AppSettings[“tenantID”];
字符串spPrinciple=ConfigurationManager.AppSettings[“spPrinciple”];
字符串spAuthUrl=ConfigurationManager.AppSettings[“spAuthUrl”];
//公共字符串TenantURL{get=>TenantURL;}
HttpClient=新的HttpClient();
KeyValuePair[]正文=新的KeyValuePair[]
{
新的KeyValuePair(“授权类型”、“客户端凭据”),
新的KeyValuePair(“客户端id,$”{clientID}@{tenantID}”),
新的KeyValuePair(“resource”、$“{spPrinciple}/{siteUrl}@{tenantID}”。替换(“https://”、“”),
新的KeyValuePair(“客户端密码”,clientSecret)
};
var content=新的FormUrlEncodedContent(正文);
var contentLength=content.ToString().Length;
字符串标记=”;
使用(httpresponsemessageresponse=wait client.PostAsync(spAuthUrl,content))
{
if(response.Content!=null)
{
string responseString=wait response.Content.ReadAsStringAsync();
JObject data=JObject.Parse(responseString);
令牌=数据值(“访问令牌”);
}
}
返回令牌;
}
私有静态ListItemCollection GetSharePointSiteList(字符串sourceSiteURL、字符串sourceListName、字符串accessToken)
{
ClientContext=GetClientContext(System.Net.WebUtility.UrlEncode(sourceSiteURL),accessToken);
Microsoft.SharePoint.Client.List oList=clientContext.Web.Lists.GetByTitle(sourceListName);
ListItemCollection collListItem=oList.GetItems(CamlQuery.CreateAllItemsQuery());
加载(collListItem);
clientContext.ExecuteQuery();
Dispose();
返回列表项;
}

正如梅尔·杰拉茨所指出的,该错误的原因是使用了未分配的SPATHURL变量

一旦分配了该变量,就不会再出现错误


(之后我将面临其他问题,但前面提到的错误已经消失)

正如梅尔·杰拉茨所指出的那样,该错误的原因是使用了未分配的spAuthUrl变量

一旦分配了该变量,就不会再出现错误


(之后我将面临其他问题,但前面提到的错误已经消失)

您从何处获得错误以及租户的值是多少?我在运行Azure函数时获得它。我在说明中提供的Sharepoint列表的值URL。当我从Sharepoint列表中获得结果时,我尝试在函数的返回值中显示Sharepoint列表的内容,但得到的却是错误。我应该单独指定租户吗?根据您的代码,您首先调用
spAuthUrl
以获取令牌。那个url正确吗?梅尔,它不正确。谢谢你指出这一点!这就是我产生错误的原因。你从哪里得到错误?租户的值是多少?我在运行Azure函数时得到它。我在说明中提供的Sharepoint列表的值URL。当我从Sharepoint列表中获得结果时,我尝试在函数的返回值中显示Sharepoint列表的内容,但得到的却是错误。我应该单独指定租户吗?根据您的代码,您首先调用
spAuthUrl
以获取令牌。那个url正确吗?梅尔,它不正确。谢谢你指出这一点!这就是我犯那个错误的原因