【问题标题】:Tweetsharp Twitter API - post multiple imagesTweetsharp Twitter API - 发布多张图片
【发布时间】:2017-10-14 11:47:25
【问题描述】:

下面的代码可以正常工作并将发布到 Twitter,但只会将一张图片发布到帐户,即使发送了三张图片。

这些只是 jpg 或 png 文件,而不是视频或动画 gif。

任何想法都将不胜感激。

    Dim service = New TwitterService(key, secret)
    service.AuthenticateWith(token, tokenSecret)

    Dim opt As New SendTweetWithMediaOptions

    Dim images As New Dictionary(Of String, IO.Stream)
    Dim myStream As FileStream

    If Len(ImgName1) > 0 Then
        myStream = New FileStream(ImgName1, FileMode.Open)
        images.Add("1", myStream)
    End If
    If Len(ImgName2) > 0 Then
        myStream = New FileStream(ImgName2, FileMode.Open)
        images.Add("2", myStream)
    End If
    If Len(ImgName3) > 0 Then
        myStream = New FileStream(ImgName3, FileMode.Open)
        images.Add("3", myStream)
    End If

    opt.Status = TweetText
    opt.Images = images

    Dim TwitterStatus = service.SendTweetWithMedia(opt)

【问题讨论】:

    标签: asp.net twitter tweetsharp


    【解决方案1】:

    我使用 TweetInvi https://github.com/linvi/tweetinvi(通过 Nuget 安装)使其工作的代码是:

    (Imports Tweetinvi)
    
    
        Auth.SetUserCredentials(key, secret, token, tokenSecret)
    
        Dim params As New Tweetinvi.Parameters.PublishTweetOptionalParameters()
        params.Medias = New List(Of Models.IMedia)
        Dim MediaIDs As String = String.Empty
    
        Dim ids As New List(Of Long)
    
        If Len(ImgName1) > 0 Then
            Dim imgFile1 As Byte() = System.IO.File.ReadAllBytes(ImgName1)
            Dim myMedia1 = Upload.UploadImage(imgFile1)
            ids.Add(myMedia1.MediaId)
            imgFile1 = Nothing
        End If
        If Len(ImgName2) > 0 Then
            Dim imgFile2 As Byte() = System.IO.File.ReadAllBytes(ImgName2)
            Dim myMedia2 = Upload.UploadImage(imgFile2)
            ids.Add(myMedia2.MediaId)
            imgFile2 = Nothing
        End If
        If Len(ImgName3) > 0 Then
            Dim imgFile3 As Byte() = System.IO.File.ReadAllBytes(ImgName3)
            Dim myMedia3 = Upload.UploadImage(imgFile3)
            ids.Add(myMedia3.MediaId)
            imgFile3 = Nothing
        End If
    
        params.MediaIds = ids
    
        Dim myTweet = Tweet.PublishTweet("blah, blah, blah", params)
    

    【讨论】:

      【解决方案2】:

      该 TweetSharp 方法调用的 API 端点不支持多张图片(我认为它从来没有支持过,但无论哪种方式,当前的文档都说不;https://dev.twitter.com/rest/reference/post/statuses/update_with_media)。

      您需要做的是使用 UploadMedia 端点上传图像并捕获每个 id,然后发送带有 id 列表的推文。

      我不确定您使用的是哪种 TweetSharp 变体。官方/原始版本在 Nuget 包中存在已知错误,虽然 repo 中的源代码已修复这些错误,但它缺乏更新的 API 支持。

      您可以尝试 TweetMoaSharp(主要由我维护),因为我相信这支持新的端点(但我已经有一段时间没有看了)。 Nuget 上还有一些 tweetsharp-alternative 和其他一些可能有支持的浮动。

      【讨论】:

      • 感谢您的回复。在网上搜索了几个小时后,我得到了和你一样的原因——不是代码,而是 API。我尝试了 TweetMoaSharp,但找不到任何关于先上传图像并取回 ID 的帮助示例。我用 TweetInvi 搞定了。干杯。
      猜你喜欢
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 2017-07-10
      • 2020-09-16
      相关资源
      最近更新 更多