简体   繁体   English

C#Twitter-上传图像-没有第三方库

[英]C# Twitter - Upload Image - No 3rd party Libraries

I have a library that I wrote a while ago that allows me to post a new status to Twitter. 我有一个图书馆,我之前写过,可以将新状态发布到Twitter。 So handling of the OAuth headers etc is all working. 因此,OAuth标头等的处理全部正常。

However, I now have a requirement to upload an image using the Twitter REST API: 但是,我现在需要使用Twitter REST API上传图像:

https://upload.twitter.com/1.1/media/upload.json

When posting a status I normally put the following in the request stream 'status=<my tweet here>' 发布状态时,我通常将以下内容放入请求流'status=<my tweet here>'

Ideally I want to post the raw image data rather than a Base64 string, however, I am having issues with each of them working. 理想情况下,我想发布原始图像数据,而不是Base64字符串,但是,它们每个都工作时遇到问题。

According to Twitter, the OAuth should only be build up from the keys starting 'oauth_' - I am only putting the following in: 根据Twitter的说法,OAuth仅应从以'oauth_'开头的键构建-我仅将以下内容放入:

parameters.Add("oauth_consumer_key", consumerKey);
parameters.Add("oauth_signature_method", "HMAC-SHA1");
parameters.Add("oauth_timestamp", Base.Methods.UNIXTimestamp);
parameters.Add("oauth_nonce", Guid.NewGuid().ToString().Replace("-", ""));
parameters.Add("oauth_version", "1.0");
parameters.Add("oauth_token", token);

Twitter says that when in doubt to use a content type of application/octet-stream - when doing this, I get a response of: Twitter表示,如果不确定要使用application/octet-stream的内容类型,那么在执行此操作时,我会收到以下答复:

Code: 38
Message: Missing Parameter Media

In fact, I also get the same response when setting the content type to multipart/form-data as suggested in other pages from Twitter 实际上,当将内容类型设置为multipart/form-data时,我也会得到相同的响应,如Twitter其他页面中所建议的那样

I have tried various combinations of add the image data to the webrequest, and all seem to fail. 我尝试了将图像数据添加到webrequest的各种组合,但似乎都失败了。

media=<my image byte data here>
Add header of 'media' with image data in the request

and as many other combinations I can think of. 以及其他我能想到的组合。 I even get the same issues when trying to send the Base64 version (which I'd rather not do). 尝试发送Base64版本时,我什至遇到相同的问题(我宁愿不这样做)。

Having read through lots of other questions I don't seem to be able to see what I am doing wrong. 读完许多其他问题后,我似乎看不到我在做什么错。

Can anyone help? 有人可以帮忙吗?

I have found the problem, and it all comes down to the content being sent in the request stream. 我发现了问题,所有问题都归结为请求流中发送的内容。

As twitter says, only send in the oauth_xxxx parameters, the content based ones are not needed for this request. 如twitter所说,仅发送oauth_xxxx参数,此请求不需要基于内容的参数。

The trick is the building up of the multipart form, I have done the following: 诀窍是建立多部分表单,我做了以下工作:

  1. I am using an HttpWebrequest, so set it up to send the multipart form headers: 我正在使用HttpWebrequest,因此将其设置为发送多部分表单标题:

     string boundary = "----" + DateTime.UtcNow.Ticks.ToString("x"); webRequest.ContentType = "multipart/form-data; boundary=" + boundary; 
  2. Build the content to be sent, which has both a prefix and a suffix to the actual image data 构建要发送的内容,该内容同时具有实际图像数据的前缀和后缀

     StringBuilder prefixData = new StringBuilder(); prefixData.Append("--"); prefixData.Append(boundary); prefixData.Append(Environment.NewLine); prefixData.Append("Content-Disposition: form-data; name=\\"media\\""); prefixData.Append(Environment.NewLine); prefixData.Append(Environment.NewLine); byte[] prefix = Encoding.UTF8.GetBytes(prefixData.ToString()); StringBuilder suffixData = new StringBuilder(); suffixData.Append(Environment.NewLine); suffixData.Append("--"); suffixData.Append(boundary); suffixData.Append("--"); byte[] suffix = Encoding.UTF8.GetBytes(suffixData.ToString()); 
  3. Join each of the data sections together to post to the stream: 将每个数据节连接在一起以发布到流中:

     byte[] data = new byte[prefix.Length + imageData.LongLength + suffix.Length]; Buffer.BlockCopy(prefix, 0, data, 0, prefix.Length); Buffer.BlockCopy(imageData, 0, data, prefix.Length, imageData.Length); Buffer.BlockCopy(suffix, 0, data, prefix.Length + imageData.Length, suffix.Length); 
  4. Write the data to the request stream as normal. 正常将数据写入请求流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在没有第三方库的Java中使用C#DLL - Using a C# DLL in Java without 3rd party libraries Windows Phone 8是否支持第三方C#库? - Does Windows Phone 8 support 3rd party C# Libraries? 在没有第三方库的情况下设置Twitter OAuth - Setting up Twitter OAuth without 3rd party libraries 在c#中用反射或类似的(没有第三方库)替换Property Getter / Setter - Replace Property Getter/Setter with Reflection or similar (no 3rd party libraries) in c# 在不使用第三方库的情况下以C#在Windows窗体上显示网络摄像头订阅源 - Display webcam feed on windows form in C# without using 3rd party libraries MonoTouch中的第三方库? - 3rd party libraries in MonoTouch? 在没有第三方库的情况下,使用C#.net 3.0或更低版本将pdf转换为tiff? - Converting pdf to tiff using C# .net 3.0 or less without 3rd party libraries? C#无法使用Http / Web / Smtp客户端(包括第三方库)进行通信 - C# Cannot communicate using Http/Web/Smtp clients, including 3rd party libraries C#AES CFB与第三方C实现的兼容性 - C# AES CFB compatibility with 3rd party C implementation 如何在Windows Phone中在没有任何第三方库的情况下使用具有OAuth身份验证的Twitter REST API 1.1 - How to use Twitter REST API 1.1 with OAuth Authentication without any 3rd party libraries in Windows Phone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM