REST API 403禁止错误

问题描述:

嗨 

以下是我的代码,它给了我"403:Forbidden Error"

Following is my code which is giving me "403: Forbidden Error"

var formDigestValue = GetDigest(); 
            string listPostBody = "{'__metadata':{'type':'SP.Data.TestListListItem'}, 'Title':'Hello World!'}";
            byte[] postByte = System.Text.Encoding.ASCII.GetBytes(listPostBody);
            SecureString ss = SecurePwd("pass word");
            HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create("https://<Site Url>/_api/lists/getByTitle('TestList')/items");
            endpointRequest.Method = "POST";
            endpointRequest.Accept = "application/json;odata=verbose";
            endpointRequest.ContentType = "application/json;odata=verbose";
            endpointRequest.Headers.Add("X-RequestDigest", formDigestValue);
            endpointRequest.UseDefaultCredentials = false;
            endpointRequest.Credentials = new SharePointOnlineCredentials("Email Add", ss);
            endpointRequest.ContentLength = postByte.Length;
            Stream postStreamBody = endpointRequest.GetRequestStream();
            postStreamBody.Write(postByte, 0, postByte.Length);
            postStreamBody.Close();
            HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();




谢谢,Parth

阅读以下
post


完整代码示例:

Read the following post.
Full code example:

public static class Utils
{
 
        public static CookieContainer GetO365CookieContainer(SharePointOnlineCredentials credentials, string targetSiteUrl)
        {
 
            Uri targetSite = new Uri(targetSiteUrl);
            string cookieString = credentials.GetAuthenticationCookie(targetSite);
            CookieContainer container = new CookieContainer();
            string trimmedCookie = cookieString.TrimStart("SPOIDCRL=".ToCharArray());
            container.Add(new Cookie("FedAuth", trimmedCookie, string.Empty, targetSite.Authority));
            return container;
 
 
        }
 
        public static SharePointOnlineCredentials GetO365Credentials(string userName, string passWord)
        {
            SecureString securePassWord = new SecureString();
            foreach (char c in passWord.ToCharArray()) securePassWord.AppendChar(c);
            SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(userName, securePassWord);
            return credentials;
        }
 
 
 
}


public static void UploadRest(string siteUrl, string libraryName, string filePath)
{
             byte[] binary = IO.File.ReadAllBytes(filePath); ;
             string fname = IO.Path.GetFileName(filePath);
             string result = string.Empty;
             string resourceUrl = siteUrl + "/_api/web/lists/getbytitle('" + libraryName + "')/rootfolder/files/add(url='" + fname + "',overwrite=true)";
 
             HttpWebRequest wreq = HttpWebRequest.Create(resourceUrl) as HttpWebRequest;
             wreq.UseDefaultCredentials = false;
             SharePointOnlineCredentials credentials = Utils.GetO365Credentials("your login", "your password");
             wreq.Credentials = credentials;
             wreq.CookieContainer = Utils.GetO365CookieContainer(credentials, siteUrl);
 
             string formDigest = GetFormDigest(siteUrl, credentials, wreq.CookieContainer);
             wreq.Headers.Add("X-RequestDigest", formDigest);
             wreq.Method = "POST";
             wreq.Timeout = 1000000;
             wreq.Accept = "application/json; odata=verbose";
             wreq.ContentLength = binary.Length;
            
 
             using (IO.Stream requestStream = wreq.GetRequestStream())
             {            
                 requestStream.Write(binary, 0, binary.Length);
             }
 
             WebResponse wresp = wreq.GetResponse();
             using (IO.StreamReader sr = new IO.StreamReader(wresp.GetResponseStream()))
             {
                 result = sr.ReadToEnd();
             }
 
 
}
public static string GetFormDigest(string siteUrl, ICredentials credentials, CookieContainer cc)
{
            string  formDigest = null;
 
            string resourceUrl = siteUrl +"/_api/contextinfo";
            HttpWebRequest wreq = HttpWebRequest.Create(resourceUrl) as HttpWebRequest;
 
            wreq.Credentials = credentials;
            wreq.CookieContainer = cc;
            wreq.Method = "POST";
            wreq.Accept = "application/json;odata=verbose";
            wreq.ContentLength = 0;
            wreq.ContentType = "application/json";
            string result;
            WebResponse wresp = wreq.GetResponse();
        
            using (IO.StreamReader sr = new IO.StreamReader(wresp.GetResponseStream()))
            {
                result = sr.ReadToEnd();
            }
 
            var jss = new JavaScriptSerializer();
            var val = jss.Deserialize<dictionary><string ,object="">>(result);
            var d = val["d"] as Dictionary<string object="" ,="">;
            var wi = d["GetContextWebInformation"] as Dictionary<string object="" ,="">;
            formDigest = wi["FormDigestValue"].ToString();
 
            return formDigest;
            
}