gpt4 book ai didi

ruby-on-rails - 如何在rails中更新推特

转载 作者:行者123 更新时间:2023-12-01 08:20:07 25 4
gpt4 key购买 nike

def post_to_twitter
message = from some where
url = URI.parse('http://twitter.com/statuses/update.xml')
req = Net::HTTP::Post.new(url.path)
req.basic_auth 'account', 'password'
req.set_form_data({'status' => message})
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
# ok
else
# false
end

end

这是 twitter 更新的代码,当我通过此操作向 twitter 发布一些更新时,它总是错误的。

我能知道哪里错了吗?

最佳答案

我建议你使用 Twitter gem

使用 API,您只需要做:

httpauth = Twitter::HTTPAuth.new('username', 'password')
client = Twitter::Base.new(httpauth)
client.update('Heeeeyyyyooo from the Twitter Gem')

并使用 OAuth(我强烈推荐):
oauth = Twitter::OAuth.new('consumer token', 'consumer secret')
oauth.authorize_from_access('access token', 'access secret')
client = Twitter::Base.new(oauth)
client.update('Heeeyyyyoooo from Twitter Gem!')

关于ruby-on-rails - 如何在rails中更新推特,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1994724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com