[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.
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."}"
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
"
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
[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).
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.
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)
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
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.
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.
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.Con开发者_如何学GotentLength = 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.
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.
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.
Just in case anyone is still trying to upload on Twitter. I am the main developer of Tweetinvi which is capable of uploading files for more than 6 months now.
Feel free to take a look at it:
Examples :
// 1 line to upload and publish
var tweet = Tweet.PublishTweetWithVideo("my tweet", File.ReadAllBytes("filepath"));
// OR WITH ADDITIONAL PARAMETERS
var uploadedVideo = Upload.UploadVideo(File.ReadAllBytes("filepath"));
tweet = Tweet.PublishTweet("my tweet", new PublishTweetOptionalParameters
{
Medias = new List<IMedia> { uploadedVideo },
Coordinates = new Coordinates(42, 42)
});
Obviously you can do more. Feel free to check the documentation : https://github.com/linvi/tweetinvi/wiki/Upload.
The TweetIt example shows how update status and post an image via update_with_media.xml, via a .NET app written in C#.
Full source is available. It does not use Twitterizer. It uses this Oauth module for managing the authentication.
Use, the tweetsharp Nuget plug in.
using (var stream = new FileStream(imagePath, FileMode.Open))
{
var result = service.SendTweetWithMedia(new SendTweetWithMediaOptions
{
Status = message,
Images = new Dictionary<string, Stream> { { "john", stream } }
});
lblResult.Text = result.Text.ToString();
}
The complete details are available at this article
Thanks
http://www.dotnetfunda.com/articles/show/3196/post-message-with-image-on-twitter-using-csharp
精彩评论