无法使用 API 在 Pinterest 上 Post

Unable to Post on Pinterest using their API

我在 Pinterest 上 post 时收到 400 Bad Request 错误消息。它使用 Postman 工作,但不能以编程方式工作。使用 C#,有没有人能够在不使用 pinsharp 包装器的情况下成功 post Pinterest 上的图钉?

private void postPinterest(string messages, string id, string usertoken, string image, string boardname, string username)
{
    string link = null;
    boardname = boardname.Replace(" ", "-");

    string board = username + "/" + boardname;
    string url = "https://api.pinterest.com/v1/pins?access_token=" + usertoken;

    StringBuilder sb = new StringBuilder();
    if (!string.IsNullOrEmpty(board))
        sb.Append("&board=" + HttpUtility.UrlEncode(board));
    if (!string.IsNullOrEmpty(messages))
        sb.Append("&note=" + HttpUtility.UrlEncode(messages));
    if (!string.IsNullOrEmpty(link))
        sb.Append("&image_url=" + HttpUtility.UrlEncode(link));

    string postdata = sb.ToString().Substring(1);
    PostData(url, postdata);
}

private object PostData(string url, string postdata)
{
    object json=null;
    try
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        //  req.Accept = "application/json";
        using (var stream = req.GetRequestStream())
        {
            byte[] bindata = Encoding.ASCII.GetBytes(postdata);
            stream.Write(bindata, 0, bindata.Length);
        }
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        string response = new StreamReader(resp.GetResponseStream()).ReadToEnd();

        json = JsonConvert.DeserializeObject<dynamic>(response);
        return json;
    }
    catch (WebException wex)
    {
        if (wex.Response != null)
        {
            using (var errorResponse = (HttpWebResponse)wex.Response)
            {
                using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                {
                    string error = reader.ReadToEnd();                          
                    return json;

                }
            }
        }
    }
    return json;

}

编辑:

使用 JSON 格式或 x-www-form-urlencoded 格式无效。

我将内容类型更改为 application/x-www-form-urlencoded,现在我收到以下错误消息。我使用 JSON 格式收到 400 Bad Request 错误:

"{\n  \"message\": \"405: Method Not Allowed\",\n  \"type\": \"http\"\n}"

问题是您正在 posting 的参数。
在 Api 中,我可以找到 board 作为参数,但 noteimage 都属于 field 参数,该参数指定 return 类型 JSON.
根据 this page 上的文档,您可以 post 这种格式

https://api.pinterest.com/v1/boards/anapinskywalker/wanderlust/pins/?
    access_token=abcde&
    limit=2&
    fields=id,link,counts,note

所以我尝试了以下方法并得到了响应

https://api.pinterest.com/v1/boards/?access_token="YourTokenWithoutQuotes"&fields=id%2Ccreator

建议您首先测试 Api 您在 PostData 函数中放置断点并检查传递的 url 格式是否正确并将其与Pininterest API Explorer.
由于您可能已经收到 authorization codeaccess token,所以我假设您的 post 函数应该可以正常工作。

public string postPinterest(string access_token,string boardname,string note,string image_url)
      {

            public string pinSharesEndPoint = "https://api.pinterest.com/v1/pins/?access_token={0}";

            var requestUrl = String.Format(pinSharesEndPoint, accessToken);
            var message = new
            {
                 board = boardname,
                 note = note,
                 image_url = image_url
            };

            var requestJson = new JavaScriptSerializer().Serialize(message);

            var client = new WebClient();
            var requestHeaders = new NameValueCollection
{

    {"Content-Type", "application/json" },
            {"x-li-format", "json" }

};
            client.Headers.Add(requestHeaders);
            var responseJson = client.UploadString(requestUrl, "POST", requestJson);
            var response = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(responseJson);
            return response;
}