Конечные точки SharePoint следуют соглашению OData.
Таким образом, вы можете использовать $select
параметры запроса, чтобы указать данные, которые вы хотите для данной сети, списка или полей и т. Д.
Такв вашем случае вы можете просто изменить конечную точку, как показано ниже:
var apiUrl = "https://[mySite].sharepoint.com/_api/web?$select=Title";
В случае, если вы хотите получить дополнительные свойства, такие как описание, логотип, Webtemplate и т. д., вы можете добавить его как:
var apiUrl = "https://[mySite].sharepoint.com/_api/web?$select=Title,Description,
SiteLogoUrl,WebTemplate";
Ссылка - Список свойств в SPO - веб-объект
Кроме того, убедитесь, что у вас есть разрешения Have full control of all site collections
, проверенные в разрешении Office 365 SharePoint Online
, как показано ниже:
![enter image description here](https://i.stack.imgur.com/G9cEB.png)
Полная версия кода, который я использую:
1) Создать AuthenticationResponse.cs
класс:
public class AuthenticationResponse
{
public string token_type { get; set; }
public string scope { get; set; }
public int expires_in { get; set; }
public int expires_on { get; set; }
public int not_before { get; set; }
public string resource { get; set; }
public string access_token { get; set; }
public string refresh_token { get; set; }
public string id_token { get; set; }
}
2) Используйте это в своем коде, как показано ниже:
string userName = "user@tenantName.onmicrosoft.com";
string password = "password";
List<KeyValuePair<string, string>> vals = new List<KeyValuePair<string, string>>();
string tenantName = "tenantName.OnMicrosoft.com";
string authString = "https://login.microsoftonline.com/" + tenantName;
string resource = "https://graph.microsoft.com";
AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
string clientId = "<client-id>";
string key = "<client-secret>";
vals.Add(new KeyValuePair<string, string>("client_id", clientId));
vals.Add(new KeyValuePair<string, string>("resource", resource));
vals.Add(new KeyValuePair<string, string>("username", userName));
vals.Add(new KeyValuePair<string, string>("password", password));
vals.Add(new KeyValuePair<string, string>("grant_type", "password"));
vals.Add(new KeyValuePair<string, string>("client_secret", key));
string url = string.Format("https://login.windows.net/{0}/oauth2/token", tenantName);
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
HttpContent content = new FormUrlEncodedContent(vals);
HttpResponseMessage hrm = httpClient.PostAsync(url, content).Result;
AuthenticationResponse authenticationResponse = null;
if (hrm.IsSuccessStatusCode)
{
Stream data = await hrm.Content.ReadAsStreamAsync();
DataContractJsonSerializer serializer = new
DataContractJsonSerializer(typeof(AuthenticationResponse));
authenticationResponse = (AuthenticationResponse)serializer.ReadObject(data);
var accessToken = authenticationResponse.access_token;
httpClient
.DefaultRequestHeaders
.Clear();
httpClient
.DefaultRequestHeaders
.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
httpClient
.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var apiUrl = "https://[mySite].sharepoint.com/_api/web?$select=Title";
}
}