【问题标题】:How to post Special character tweet using asp.net API?如何使用 asp.net API 发布特殊字符推文?
【发布时间】:2016-09-04 00:40:38
【问题描述】:

我正在使用 Given 下面的代码在 Twitter 上发布推文。但是当我们将它上传到服务器上时,特殊字符(!、:、$ 等)不会在 Twitter 上发布推文。此代码在本地系统中运行良好

string key = "";
string secret = "";
string token="";
string tokenSecret="";
try
{           
string localFilename = HttpContext.Current.Server.MapPath("../images/").ToString();

            using (WebClient client = new WebClient())
            {
                client.DownloadFile(imagePath, localFilename);
            }
            var service = new TweetSharp.TwitterService(key, secret);
            service.AuthenticateWith(token, tokenSecret);
            // Tweet wtih image
            if (imagePath.Length > 0)
            {
                using (var stream = new FileStream(localFilename, FileMode.Open))
                {
                    var result = service.SendTweetWithMedia(new SendTweetWithMediaOptions
                    {
                        Status = message,
                        Images = new Dictionary<string, Stream> { { "name", stream } }
                    });
                }                   
            }
            else // just message
            {
                var result = service.SendTweet(new SendTweetOptions
                {
                    Status = HttpUtility.UrlEncode(message)
                });
            } 

    }
    catch (Exception ex)
    {
        throw ex;
    }

【问题讨论】:

    标签: asp.net tweetsharp


    【解决方案1】:

    statuses/update_with_media API 端点实际上已被 Twitter 弃用,不应使用 (https://dev.twitter.com/rest/reference/post/statuses/update_with_media)。

    当推文同时包含“特殊字符”和图像(两者都可以正常工作,但不能同时使用)时,TweetSharp 在使用此方法时也会遇到一些问题。我不知道为什么,也无法修复它,我很确定这与 OAuth 签名有关。

    作为一种解决方案,我建议您使用 TweetMoaSharp(TweetSharp 的一个分支)。它已更新以支持新的 Twitter API 用于处理推文中的媒体,如果您使用新的东西,它将在这种情况下工作。

    基本上,您使用新的 UploadMedia 方法上传每个媒体项目,这将返回一个“媒体 ID”。然后,您使用普通的“SendTweet”方法并提供媒体 ID 列表以及其他状态详细信息。 Twitter 会在推文发布时将媒体附加到推文中,并且在同时存在特殊字符和图像时才会起作用。

    【讨论】:

      【解决方案2】:

      除了 TweetMoaSharp 之外,您还可以通过以下代码使用 Tweetinvi:

      var binary = File.ReadAllBytes(@"C:\videos\image.jpg");
      var media = Upload.UploadMedia(binary);
      
      var tweet = Tweet.PublishTweet("hello", new PublishTweetOptionalParameters
      {
          Medias = {media}
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-01
        • 1970-01-01
        • 2019-07-19
        相关资源
        最近更新 更多