I was trying to test creating folder under root folder in sharepoint online using access token and REST API using HTTPWebRequest classes from console application.
I don't want to use any sharepoint assemblies as since I can't actually use this in my final code. But always getting 403 Forbidden exception when getting the response.
Can any one kindly suggest if any useful reference documentation for this issue?
Code I am using:
void CreateFolder(string token, string library, string folder)
{
string sharePointSite = "https://xxx.sharepoint.com/sites/xxx";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sharePointSite + "/_api/lists/getByTitle('" + library + "')");
//HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sharepointURL + "/_api/lists/getByTitle('" +library + "')/rootfolder/folders/add(url ='" +folder + "')");
request.Method = "POST";
request.Accept = "application/json; odata=verbose";
//request.Headers.Add(HttpRequestHeader.Authorization, "Bearer" + token);
myWebRequest.Headers.Add("Authorization", "Bearer");
ASCIIEncoding encoding = new ASCIIEncoding();
//byte byte1 = encoding.GetBytes(request);
request.ContentLength = 0;
Console.WriteLine(request);
try
{
if (request.GetResponse().ToString() != null)
{
using (WebResponse response = request.GetResponse())
{
Console.WriteLine(response);
Console.ReadKey();
}
// Do something if the resource has changed.
}
}
catch (WebException wex)
{
HttpWebResponse httpResponse = wex.Response as HttpWebResponse;
if (httpResponse.StatusCode != (HttpStatusCode.NotModified))
{ // resource was not modified.
Console.WriteLine("Error occured from line 195");
}
// Something else happened. Rethrow or log.
//throw;
}
catch (Exception ex)
{ // Something else happened. Rethrow or log
Console.WriteLine("Error occured from line 203");
}