'提供了无效的请求URI。连接到AF中的SP的请求URI必须是绝对URI或必须设置BaseAddress

时间:2018-08-08 13:34:58

标签: c# office365 azure-functions csom

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

  

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

在这里粘贴代码,请让我知道是否有人可以检测到任何内容

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

1 个答案:

答案 0 :(得分:1)

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

分配了该变量后,错误将不再出现

(此后我将面临其他问题,但上述错误消失了)