使用 Rails 将带有状态的图像上传到 Twitter [英] Uploading image with status to Twitter using Rails
问题描述
我正在使用一个名为 prepare_access_token
的函数来联系 Twitter api 以执行读取、写入功能.
下面是我用来更新推特消息的代码
<前><代码>@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_keyconsumer_secret = Rails.application.config.consumer_secret消费者 = OAuth::Consumer.new(consumer_key, consumer_secret,{:网站=>"http://api.twitter.com",:scheme =>:标题})# 现在从传递的值创建访问令牌对象token_hash = {:oauth_token =>oauth_token,:oauth_token_secret =>oauth_token_secret}access_token = OAuth::AccessToken.from_hash(consumer, token_hash)返回 access_token结尾
这适用于没有附加图像的状态更新,但是当我尝试使用如下图像附件更新状态时
@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 创建状态."}
我做错了什么?请提出建议.
对于消费者:站点,尝试将其设置为 http://upload.twitter.com 而不是 http://api.twitter.com.api站点不支持媒体上传.
I am using a function called prepare_access_token
to contact Twitter api to perform Read, Write functions.
Below is the code i use to update twitter message
@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
This works for status update without attached image , but when i try to update status with image attachment like below
@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")
it shows like {"request":"/1/statuses/update_with_media.json","error":"Error creating status."}
what am i doing wrong? please suggest.
For the consumer:site, try setting it to http://upload.twitter.com instead of http://api.twitter.com. the api site does not support media upload.
这篇关于使用 Rails 将带有状态的图像上传到 Twitter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!