'An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set' 连接到 AF 中的 SP 异常

'An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set' exception connecting to a SP in an AF

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

'An invalid request URI was provided. The request URI must either be an absolute >URI or BaseAddress must be set'

我把代码贴在这里,如果有人能检测到任何东西,请告诉我

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;
}

正如 Mel Gerats 指出的那样,该错误的原因是使用了未分配的 spAuthUrl 变量

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

(之后我会遇到其他问题,但上述错误消失了)