【问题标题】:Uploading image with status to Twitter using Rails使用 Rails 将带有状态的图像上传到 Twitter
【发布时间】:2012-07-08 13:38:18
【问题描述】:

我正在使用一个名为 prepare_access_token 的函数来联系 Twitter api 以执行读取、写入功能。

下面是我用来更新 twitter 消息的代码

@tweet = "Rails Rails" @access_token = prepare_access_token(@oauth_token, @oauth_secret) @response = @access_token.request(:post, "http://api.twitter.com/1/statuses/update.json", :status => @tweet)
def prepare_access_token(oauth_token, oauth_token_secret)

  consumer_key = Rails.application.config.consumer_key
  consumer_secret = Rails.application.config.consumer_secret  

  consumer = OAuth::Consumer.new(consumer_key, consumer_secret,
    { :site => "http://api.twitter.com",
      :scheme => :header
    })
  # now create the access token object from passed values
  token_hash = { :oauth_token => oauth_token,
                 :oauth_token_secret => oauth_token_secret
               }
  access_token = OAuth::AccessToken.from_hash(consumer, token_hash )
  return access_token
end

这适用于没有附加图像的状态更新,但是当我尝试使用如下图像附件更新状态时

@access_token = prepare_access_token(@oauth_token, @oauth_secret)
@response = @access_token.request(:post,  "https://upload.twitter.com/1/statuses/update_with_media.json"  , :media => $image , :status  => "Status")

它显示为{"request":"/1/statuses/update_with_media.json","error":"Error creating status."}

我做错了什么?请提出建议。

【问题讨论】:

  • 你有解决这个问题的办法吗?

标签: ruby-on-rails twitter


【解决方案1】:

对于 consumer:site,尝试将其设置为 http://upload.twitter.com 而不是 http://api.twitter.com。 api站点不支持媒体上传。

【讨论】:

    【解决方案2】:

    与 Twitter API 的其他 REST 方法不同,您必须将此 POST https://upload.twitter.com/1/statuses/update_with_media.json 方法的 Content-Type 设置为 multipart/form-data

    【讨论】:

    • 我怎样才能用这个请求调用来设置这个。我尝试使用:content-type => "multipart/form-data",但没有运气!!
    • 我只知道 Twitter API,而不是 RoR。 Content-Type 是一个 HTTP 标头,所以我猜你确实喜欢 HTTP 请求中的其他 HTTP 标头。
    • 好的.. 但即使在设置内容类型之后 - 同样的错误@response = @access_token.post("https://upload.twitter.com/1/statuses/update_with_media.json", { :status => "stat", :media => @image }, {'Content-Type' => 'multipart/form-data'})
    猜你喜欢
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 2023-03-27
    • 2012-06-28
    • 2015-01-10
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    相关资源
    最近更新 更多