我想更新文件属性,但始终收到403错误. [英] I want to update file properties but I keep getting 403 error.

查看:65
本文介绍了我想更新文件属性,但始终收到403错误.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Uri updateFieldUrl =新的Uri(webUrl +"/_api/web/GetFileByServerRelativeUrl('/Requirements/docTwo.docx')/ListItemAllFields/Description");

            var body ="{'__metadata':{'type':'SP.ListItem'},'Description':'Added'}';
               
           //Uri WebUri =新的Uri(updateFieldUrl);

            var client = new WebClient();

                client.Credentials = cred;
                client.Headers [HttpRequestHeader.Accept] ="application/json; odata = verbose";
                client.Headers [HttpRequestHeader.ContentType] ="application/json; odata = verbose";
                client.Headers ["X-RequestDigest"] = GetFormDigestValueMetadata(webUrl,cred,cc);
                client.Headers ["If-Match"] ="*";
                client.Headers ["X-Http-Method"] ="MERGE";
                //client.Proxy.Credentials = cred;
                client.Headers.Remove(期望");
                System.Net.ServicePointManager.Expect100Continue =假;

            
           试试
            {
               字符串json = client.UploadString(updateFieldUrl,body.ToString());
            }
            catch(异常e)
            {
                MessageBox.Show(e.ToString());
            }

 Uri updateFieldUrl = new Uri(webUrl + "/_api/web/GetFileByServerRelativeUrl('/Requirements/docTwo.docx')/ListItemAllFields/Description");

            var body = "{'__metadata': {'type':'SP.ListItem'},'Description':'Added'}";
               
           //Uri WebUri = new Uri(updateFieldUrl);

            var client = new WebClient();

                client.Credentials = cred;
                client.Headers[HttpRequestHeader.Accept] = "application/json;odata=verbose";
                client.Headers[HttpRequestHeader.ContentType] = "application/json;odata=verbose";
                client.Headers["X-RequestDigest"] = GetFormDigestValueMetadata(webUrl, cred, cc);
                client.Headers["If-Match"] = "*";
                client.Headers["X-Http-Method"] = "MERGE";
                //client.Proxy.Credentials = cred;
                client.Headers.Remove("Expect");
                System.Net.ServicePointManager.Expect100Continue = false;

            
            try
            {
                string json = client.UploadString(updateFieldUrl,body.ToString());
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }

推荐答案

请检查代码段中是否有有效的FormDigest,它需要对/_api/contextinfo/

Please check if you have got valid FormDigest in the code snippet, it needs to do a POST Call to /_api/contextinfo/

 static async Task<string> GetDigestAsync(string endPoint, HttpClient client)
        {
            string digest = null;
            HttpResponseMessage response = await client.PostAsync(endPoint, new StringContent(""));
            if (response.IsSuccessStatusCode)
            {
                digest = await response.Content.ReadAsStringAsync();
            }
            JObject rss = JObject.Parse(digest);

            return rss["d"]["GetContextWebInformation"]["FormDigestValue"].ToString();
        }

    static async Task RunAsync()
        {
            var username = "administrator";
            var password = "P@ssw0rd";
            var domain = "contoso2016";
            var handler = new HttpClientHandler();
            handler.Credentials = new System.Net.NetworkCredential(username, password, domain);
            const string RESTURL = "{0}/_api/web/getfilebyserverrelativeurl('{1}')/ListItemAllFields";
            string rESTUrl = string.Format(RESTURL, "http://sp2016/", "/Shared Documents/CTSLabWSUS.log");
            var client = new HttpClient(handler);
            client.BaseAddress = new Uri("http://sp2016/");
            
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
            var digest = await GetDigestAsync("/_api/contextinfo", client);
            Console.WriteLine(digest);
            client.DefaultRequestHeaders.Add("X-HTTP-Method", "MERGE");
            client.DefaultRequestHeaders.Add("If-Match", "*");

            client.DefaultRequestHeaders.Add("X-RequestDigest", digest);

            string metadata = "{'__metadata': { 'type': 'SP.Data.Shared_x0020_DocumentsItem' }, 'Description': 'whatever'}";
            string payload = JsonConvert.SerializeObject(metadata);
            HttpResponseMessage response = await client.PostAsync(rESTUrl, new StringContent(payload, System.Text.Encoding.UTF8, "application/json")).ConfigureAwait(false);
            response.EnsureSuccessStatusCode();

        }

     static void Main(string[] args)
        {
            RunAsync().GetAwaiter().GetResult();
            Console.ReadLine();

            
        }

并确保您使用的Body数据具有有效的__metadata,您可以使用Rest Call在浏览器中检入:

And make sure the Body data you used has valid __metadata, you can check in browser with Rest Call:

谢谢

最好的问候


这篇关于我想更新文件属性,但始终收到403错误.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆