使用 Sharepoint Rest API 创建列表项;结果是 400 Bad request

Create list item with Sharepoint Rest API; Result is 400 Bad request

我实现了以下代码:

using Microsoft.SharePoint.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net;
using System.Security;
using System.Text;

namespace Sharepoint_sandbox {
   class Program {
      private static CookieContainer GetO365CookieContainer (SharePointOnlineCredentials credentials, string targetSiteUrl){
         CookieContainer container = null;

         Uri targetSite = new Uri (targetSiteUrl);
         string cookieString = credentials.GetAuthenticationCookie (targetSite);
         if (cookieString != null) {
            string trimmedCookie = cookieString.TrimStart ("SPOIDCRL=".ToCharArray ());
            container = new CookieContainer ();
            container.Add (new Cookie ("SPOIDCRL", trimmedCookie, string.Empty, targetSite.Authority));
         }

         return container;
      }

      private static SharePointOnlineCredentials GetO365Credentials (string userName, string password) {
         SecureString securePassword = new SecureString ();
         foreach (char c in password.ToCharArray ())
            securePassword.AppendChar (c);

         return new SharePointOnlineCredentials (userName, securePassword);
      }

      private static string GetFormDigest (string siteUrl, ICredentials credentials, CookieContainer cc) {
         string formDigest = null;

         string resourceUrl = siteUrl + "/_api/contextinfo";
         HttpWebRequest httpWebRequest = HttpWebRequest.Create (resourceUrl) as HttpWebRequest;
         httpWebRequest.Credentials = credentials;
         httpWebRequest.CookieContainer = cc;
         httpWebRequest.Timeout = 10000;
         httpWebRequest.Method = "POST";
         httpWebRequest.ContentType = "application/json; odata=verbose";
         httpWebRequest.Accept = "application/json;odata=verbose";
         httpWebRequest.ContentLength = 0;
         httpWebRequest.ContentType = "application/json";
         string result;
         using (WebResponse webResponse = httpWebRequest.GetResponse ()) {
            using (System.IO.StreamReader sr = new System.IO.StreamReader (webResponse.GetResponseStream ())) {
               result = sr.ReadToEnd ();
            }
         }
         var dObject = (JObject)JsonConvert.DeserializeObject (result);
         foreach (var item in dObject["d"].Children ()){
            formDigest = item.First["FormDigestValue"].ToString ();
         }

         return formDigest;
      }

      private static Tuple<string, List<string>> CreateListItem (string site, string userName, string password, string resourceUrl, string content) {
         HttpWebRequest httpWebRequest = HttpWebRequest.Create (resourceUrl) as HttpWebRequest;
         httpWebRequest.UseDefaultCredentials = false;
         SharePointOnlineCredentials credentials = GetO365Credentials (userName, password);
         httpWebRequest.Credentials = credentials;
         httpWebRequest.CookieContainer = GetO365CookieContainer (credentials, site);
         string formDigest = GetFormDigest (site, credentials, httpWebRequest.CookieContainer);
         httpWebRequest.Method = "POST";
         httpWebRequest.Headers.Add ("X-RequestDigest", formDigest);
         httpWebRequest.Timeout = 10000;
         httpWebRequest.ContentType = "application/json; odata=verbose";
         httpWebRequest.Accept = "application/json; odata=verbose";
         httpWebRequest.ContentLength = 0;

         List<string> errorMessages = new List<string> ();
         string response = "";
         try {
            byte[] binary = Encoding.Unicode.GetBytes(content);
            httpWebRequest.ContentLength = binary.Length;
            using (System.IO.Stream requestStream = httpWebRequest.GetRequestStream ()) {
               requestStream.Write (binary, 0, binary.Length);
            }

            using (WebResponse webResponse = httpWebRequest.GetResponse ()) {
               using (System.IO.StreamReader sr = new System.IO.StreamReader (webResponse.GetResponseStream ())) {
                  response = sr.ReadToEnd ();
               }
            }
         }
         catch (WebException e) {
            errorMessages.Add (e.Message);
            if (e.Response != null && e.Response.GetResponseStream () != null) {
               using (System.IO.StreamReader sr = new System.IO.StreamReader (e.Response.GetResponseStream ())) {
                  errorMessages.Add (sr.ReadToEnd ());
               }
            }
         }

         return new Tuple<string, List<string>> (response, errorMessages);
      }

      static void Main (string[] args) {
         string site = "https://*******";
         string user = "******";
         string password = "******";

         string resourceUrl = site + "/_api/Web/Lists(guid'0818cf14-4028-4c7d-adbc-489adb1c0cb4')/Items";
         string content = "{ '__metadata': { 'type':'SP.Data.Test_x005f_1ListItem' }, 'Title': 'TestItem'}";

         Tuple<string, List<string>> result = CreateListItem (site, user, password, resourceUrl, content);

         Console.Write (result.Item1 + "\n" + string.Join ("\n", result.Item2));
         Console.ReadKey ();
      }
   }
}

结果是:

A távoli kiszolgáló a következő hibát küldte vissza: (400) Hibás kérelem.
{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"hu-HU","value":"Invalid JSON. The property name '' is not valid. The name of a property cannot be empty."}}}

匈牙利语英文文本: 远程服务器返回错误:(400) 错误请求。

我不明白哪个 属性 是空的。如果内容第一个和最后一个字符不是 { 和 } 那么错误是 "Invalid JSON. A token was not recognized in the JSON content." 如果内容为空 {} 或 {} 之间的任何其他字符串,则错误与上述相同(属性 名称 '' 无效。...")

你能帮帮我吗? 谢谢

将编码从 Unicode 更改为 UTF8 解决问题:

发件人:

byte[] binary = Encoding.Unicode.GetBytes(content);

收件人:

byte[] binary = Encoding.UTF8.GetBytes(content);

我更愿意使用 StreamWriter class instead of Encoding class 来避免任何编码问题。

在您的示例中设置请求正文替换:

byte[] binary = Encoding.Unicode.GetBytes(content);
httpWebRequest.ContentLength = binary.Length;
using (System.IO.Stream requestStream = httpWebRequest.GetRequestStream())
{
     requestStream.Write(binary, 0, binary.Length);
}

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    streamWriter.Write(content);
}