怎样才能将照片上传到Twitter的API函数的日志状态/ update_with_media函数、状态、照片、日志

2023-09-04 06:17:19 作者:繁华散零落散为止丶

[更新2011-09-21 11:19] 我已经修正了一个问题,一个问题,那就是格林尼治标准​​时间/ GMT + 2 我的本地时间为GMT + 2,和Twitter使用GMT + 0 / UTC。这是导致出界错误的时间戳。

[Update 2011-09-21 11:19] I've fixed one problem that was causing a problem and that was GMT/GMT+2 My local time is at GMT+2, and twitter is using GMT+0/UTC. This was causing a timestamp out of bounds error.

我现在已经修复了这个问题,现在面临的下一个问题:   串(96){请求:/ 1 /状态/ update_with_media.json,错误:无法验证与OAuth的}

I've now fixed that problem and now facing the next problem: string(96) "{"request":"/1/statuses/update_with_media.json","error":"Could not authenticate with OAuth."}"

这是通过Themattharris PHP库产生的请求头(使用curl):

This is the request header generated through Themattharris PHP library (using curl):

 ["request_header"]=>
    string(587) "POST /1/statuses/update_with_media.json HTTP/1.1

User-Agent: themattharris' HTTP Client

Host: upload.twitter.com

Accept: */*

Authorization: OAuth oauth_consumer_key="L9AWIB6jvxm3Req1XWHxJA", oauth_nonce="9e41eea04e2dcc2b67784b35c27d8740", oauth_signature="N2Y4YzYwMDg1YjBmZTA1NDgwNDdjOGY3MDI4YjdiNWE3M2E5ZTA5YQ%3D%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1316596483", oauth_token="373846001-bJYH4C47vk34yfIH3XbpvvfZjd2JrIX9OlO5MyO2", oauth_version="1.0"

Content-Length: 10024

Content-Type: multipart/form-data; boundary=----------------------------3ddd9c41c091



"

我相当肯定,我使用了正确的密码(复制pastaed从我的应用程序的微博开发网页)在那里,我也产生了我的私人秘密凭证

I am fairly sure that I used the correct secrets (copy-pastaed from the twitter dev pages of my application) There I also generated my private secret token

[更新2011-09-21〜10:45] 所以,我周围用PHP LIB得到(改写为C ++ / CLI)和我的C ++的lib现在产生完全相同的结果作为PHP库。 (授权头字符串)。

[Update 2011-09-21 ~10:45] So I've got around using a PHP lib (rewritten to C++/CLI) and my C++ lib now produces the exact same results as the PHP Lib. (The Authorization header string).

由于某种原因,我不断收到一个状态:(401)未经授权。 我使用System.Net.WebClient提出请求。并采用UploadValues​​,好了上传的值。

For some reason I keep getting a status:(401) Unauthorized. I am using System.Net.WebClient to make the request. And using UploadValues to, well upload the values.

如果我使用类似UploadData或使用Web客户端的WebRequest的,而不是我得到一个状态:(500)内部服务器错误

If I use something like UploadData or using WebRequest instead of WebClient I get a Status:(500) Internal Server error.

我可以提供,如果需要,我生成的字符串和头文件(但我当然不会给出来的秘密)

I can provide all the strings and headers that I generate if needed (but I of course won't give out the secrets)

这是例如什么我basestring的和授权字符串如下:

This is for example what my basestring and authorization string look like:

basestring的:   POST&https%3A%2F%2Fupload.twitter.com%2F1%2Fstatuses%2Fupdate_with_media.json&oauth_consumer_key%3DL9AWIB6jvxm3Req1XWHxJA%26oauth_nonce%3Ddb0ca1f6c926c1d3ae0d9cbb2c349ae2%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1316538778%26oauth_token%3D373846001-bJYH4C47vk34yfIH3XbpvvfZjd2JrIX9OlO5MyO2%26oauth_version%3D1.0

BaseString: POST&https%3A%2F%2Fupload.twitter.com%2F1%2Fstatuses%2Fupdate_with_media.json&oauth_consumer_key%3DL9AWIB6jvxm3Req1XWHxJA%26oauth_nonce%3Ddb0ca1f6c926c1d3ae0d9cbb2c349ae2%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1316538778%26oauth_token%3D373846001-bJYH4C47vk34yfIH3XbpvvfZjd2JrIX9OlO5MyO2%26oauth_version%3D1.0

授权:OAuth的oauth_consumer_key =L9AWIB6jvxm3Req1XWHxJA   oauth_nonce =db0ca1f6c926c1d3ae0d9cbb2c349ae2   oauth_signature =OGI4ZDVkYjZjNjA3YWMyZGYxNWYzZDBhNDE0ODcwMzRkMDE4OWZiYQ%3D%3D,   oauth_signature_method =HMAC-SHA1,oauth_timestamp =1316538778,   oauth_token =373846001-bJYH4C47vk34yfIH3XbpvvfZjd2JrIX9OlO5MyO2   oauth_version =1.0

Authorization: OAuth oauth_consumer_key="L9AWIB6jvxm3Req1XWHxJA", oauth_nonce="db0ca1f6c926c1d3ae0d9cbb2c349ae2", oauth_signature="OGI4ZDVkYjZjNjA3YWMyZGYxNWYzZDBhNDE0ODcwMzRkMDE4OWZiYQ%3D%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1316538778", oauth_token="373846001-bJYH4C47vk34yfIH3XbpvvfZjd2JrIX9OlO5MyO2", oauth_version="1.0"

下面是旧的文字,这是无效的任何更

Below is the old text, which is not valid any more.

我可能是一个简单的问题,但我只是不明白这一点。 我使用 Twitterizer 让我所要求的秘密令牌。该库还提供了一种方法来更新你的Twitter有一个新的鸣叫,但它不支持新update_with_media电话。

I might be a simple question, but I just can't figure this out. I am using Twitterizer to get me the required secret tokens. This library also provides a way to update your twitter with a new tweet, but it does not support the new update_with_media call.

我尝试了几种方法,通过使用WebRequest类和其他方式使用Web客户端的一种方式(伪C# - code):

I've tried several ways, one way by using WebRequest and the other way by using WebClient (pseudo-C#-code):

string URL = "https://upload.twitter.com/1/statuses/update_with_media.json?oauth_consumer_secret=[mysecretgoeshere]&oauth_token_secret=[usersecretgoeshere]";
System.Net.ServicePointManager.Expect100Continue = false;
Byte[] fileData = File.ReadAllBytes(pathToFile);

#if useWebRequest
    string postString = "status={statusgoeshere}&image[]={Encoding.ASCII.GetString(fileData)}";
    Byte[] postData = Encoding.ASCII.GetBytes(postString);

    WebRequest wr = WebRequest.Create(URL);

    wr.Method = "POST";
    wr.ContentType = "multipart/form-data"; //as required by twitter API
    wr.ContentLength = postData.Length;

    IO.Stream rqstream = wr.GetRequestStream();
    rqstream.Write(postData,0,postData.Length);
    rqstream.Close();

    wr.GetResponse(); //returns status 500 internal server error
#else
    WebClient client = new WebClient();
    NameValueCollection postData = new NameValueCollection();

    postData.Add("status", "{statusgoeshere}");
    postData.add("media[]", Encoding.ASCII.GetString(fileData));

    client.UploadValues(URL, "POST", postData); //returns status 401 not authorized
#endif

我不知道我做错了(如果我做了什么不对的)。这两种方式似乎在我眼里有效的,但我可能会丢失一些东西。

I wonder what I am doing wrong (if I am doing something wrong that is). Both ways seem valid in my eyes, but I might be missing some things.

EDIT1: 我注意到我传递了错误的方式宣誓参数,将尝试和更新的部分。

I've noticed I am passing the oath parameters in the wrong way, will try and update that part.

推荐答案

这是我没有添加任何文件上传到Twitterizer但究其原因是因为它需要修改WebRequestBuilder类(即增加了OAuth的头和标志请求)处理多部分POST请求。我试图做出必要的修改,以此类几次,但每次他们造成了不稳定性在图书馆休息,主要是由于对如何Twitter的API预计多部分请求签名缺乏文档。

The reason that I haven't added any file uploads to Twitterizer yet is because it requires modification of the WebRequestBuilder class (that adds the OAuth header and signs requests) to handle multipart POST requests. I have attempted to make the needed modifications to this class a few times, but each time they resulted in instability in the rest of the library, mostly due to lack of documentation on how the Twitter API expects multipart requests to be signed.

有点然而,最近一个文档发布到 Twitter的API门户详细说明这些要求。如果您想尝试,以实现自己这个逻辑,那将是一个非常宝贵的资源。否则,你可以等待。我计划做的修改即将正确支持文件上传鉴于此信息。

Somewhat recently, however, a document was posted to the Twitter API portal detailing these requests. If you want to try to implement this logic on your own, that will be an invaluable resource. Otherwise, you can wait. I plan on making the modifications soon to properly support file uploads in light of this information.