Trying to build an external c# web app that pulls information from SharePoint online using the rest api.
rest call: https://mysite.sharepoint.com/sites/mySubsite/_api/web/lists/GetByTitle('My Repo')/items pulls back the information I need from the browser.
Tried with the following code
private static async Task<string>getWebTitle(webUrl)
{
string webUrl="MYSHAREPOINTSITE";
//Creating Password
string PWD = "MYPASSWORD";
string USER = "MYUSER";
string RESTURL = "_api/web/lists/GetByTitle('My Repo')/items";
//Creating Credentials
var passWord = new SecureString();
foreach (var c in PWD) passWord.AppendChar(c);
var credential = new SharePointOnlineCredentials(USER, passWord);
using (var handler = new HttpClientHandler() { Credentials = credential })
{
//Getting authentication cookies
Uri uri = new Uri(webUrl);
handler.CookieContainer.SetCookies(uri, credential.GetAuthenticationCookie(uri));
//Invoking REST API
using (var client = new HttpClient(handler))
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(string.Format(RESTURL, webUrl)).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
string jsonData = await response.Content.ReadAsStringAsync();
return jsonData;
}
}
}
However it always returns 403 forbidden. Any help in resolving this would be great. Thanks in advance.