【发布时间】: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