【发布时间】:2015-12-09 06:48:07
【问题描述】:
如何在 C# 中通过 POST statuses/update_with_media 将图片发布到 Twitter
https://dev.twitter.com/docs/api/1/post/statuses/update_with_media
【问题讨论】:
标签: c# twitter twitter-oauth twitter4j
如何在 C# 中通过 POST statuses/update_with_media 将图片发布到 Twitter
https://dev.twitter.com/docs/api/1/post/statuses/update_with_media
【问题讨论】:
标签: c# twitter twitter-oauth twitter4j
好的。我将在假设您知道如何使用他们的 REST api 简单地“推文”文本的情况下回答这个问题。如果您不这样做,请告诉我,我也可以提供帮助。
显然,以下两种方法对它们有更多作用,但它们确实详细显示了 REST 请求中需要额外完成的操作。需要注意的是,在这部分请求https://upload.twitter.com/1/statuses/update_with_media.json中没有使用任何查询字符串@
这些方法来自我编写的 OAuth 库,用于处理对 Twitter 和 LinkedIn 等各种 REST API 的 api 调用。
关键是如何在请求流中添加推文和照片信息。我必须以 link 为他的例子归功于个人......它肯定影响了我的 PrepareTwitterDataRequest() 方法。我也意识到这个例子是在 VB.NET 中,你要求 C#...如果你不能翻译试试this.
Friend Function ApiWebRequest(ByVal method As String, ByVal url As String, ByVal fileRequest As OAuthFileRequest) As String
Dim request As HttpWebRequest = Me.GenerateCoreWebRequest(url, method)
request.Headers.Add("Authorization", Me.GetAuthHeaderValue(New Uri(url), method))
Select Case Me.ApiType
Case OAuthApiType.Twitter
Me.PrepareTwitterDataRequest(request, fileRequest)
Case OAuthApiType.LinkedIn
Me.PrepareLinkedInDataRequest(request, fileRequest)
Case OAuthApiType.Other
Me.PrepareDataRequest(request, fileRequest)
End Select
Return Me.ProcessWebRequest(request)
End Function
Private Sub PrepareTwitterDataRequest(ByRef request As HttpWebRequest, ByVal fileRequest As OAuthFileRequest)
Dim shortFileName As String = fileRequest.FileShortName
Dim fileContentType As String = Me.GetMimeType(shortFileName)
Dim message As String = fileRequest.Message
Dim encoding As Text.Encoding = Text.Encoding.GetEncoding("iso-8859-1")
Dim fileHeader As String = String.Format("Content-Disposition: file; " & "name=""media""; filename=""{0}""", shortFileName)
Dim boundary As String = DateTime.Now.Ticks.ToString("x")
Dim separator As String = String.Format("--{0}", boundary)
Dim footer As String = String.Format("\r\n{0}--\r\n", separator)
Dim contents As New Text.StringBuilder()
contents.AppendLine(separator)
contents.AppendLine("Content-Disposition: form-data; name=""status""")
contents.AppendLine()
contents.AppendLine(message)
contents.AppendLine(separator)
contents.AppendLine(fileHeader)
contents.AppendLine(String.Format("Content-Type: {0}", fileContentType))
contents.AppendLine()
Dim contentsBuffer As Byte() = encoding.GetBytes(contents.ToString())
Dim footBuffer As Byte() = encoding.GetBytes(footer)
'collect all byte() into one stream
Dim byteCollector As New MemoryStream
byteCollector.Write(contentsBuffer, 0, contentsBuffer.Length)
byteCollector.Write(fileRequest.FileData, 0, fileRequest.FileData.Length)
byteCollector.Write(footBuffer, 0, footBuffer.Length)
'dump collector stream into byte()
Dim requestBytes As Byte() = byteCollector.ToArray()
'close/dispose the collector
byteCollector.Close()
byteCollector.Dispose()
request.ContentType = (String.Format("multipart/form-data; boundary=""{0}""", boundary))
request.ContentLength = requestBytes.Length
Using reqStream As IO.Stream = request.GetRequestStream()
reqStream.Write(requestBytes, 0, requestBytes.Length)
End Using
End Sub
【讨论】:
这是一个名为 TweeterSharp 的 API,可用于将消息与图像一起发送到 twitter,下面是代码示例。
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();
}
完整描述位于 - 复制自 Post message with image on twitter using C#
【讨论】: