如何通过C#
中的POST状态/ update_with_media将图像发布到Twitterhttps://dev.twitter.com/docs/api/1/post/statuses/update_with_media
答案 0 :(得分:2)
确定。我将在假设你知道如何使用他们的REST api简单地“推文”文本的情况下回答这个问题。如果你不这样做,请告诉我,我也可以帮助你。
显然,以下两种方法对它们有很多,但它们确实详细显示了在REST请求中需要另外完成的工作。请注意,在请求https://upload.twitter.com/1/statuses/update_with_media.json
的这一部分中不使用任何查询字符串,这一点很重要这些是我编写的OAuth库中的方法,它处理api调用各种REST API,如Twitter和LinkedIn。
关键是如何在请求流中添加推文和照片信息。我必须赞扬这个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
答案 1 :(得分:2)
这是一个名为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();
}
复制