【问题标题】:'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'提供了无效的请求 URI。请求 URI 必须是绝对 URI 或必须设置 BaseAddress 连接到 AF 中的 SP 的异常
【发布时间】:2019-01-15 19:15:23
【问题描述】:

我正在尝试连接到 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;
}

【问题讨论】:

  • 从哪里得到错误,tenantUrl 的值是多少?
  • 我在运行 Azure 函数时得到它。我在描述中提供的 Sharepoint 列表的值 URL。当我从 Sharepoint 列表中获取结果时,我尝试在函数的返回值中显示 Sharepoint 列表的内容,但得到错误我是否应该单独指定租户,即someplace.sharepoint.com
  • 根据您的代码,您首先调用spAuthUrl 以获取令牌。那个网址对吗?
  • 梅尔,这是不正确的。感谢您指出这一点!这就是我出现该错误的原因

标签: c# office365 azure-functions csom


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-30
    • 2020-11-22
    • 1970-01-01
    • 2020-01-07
    • 2021-07-30
    • 1970-01-01
    • 2018-08-05
    • 2018-10-01
    相关资源
    最近更新 更多