如何使用访问令牌在rails上使用访问令牌在Ruby上进行GET和POST

问题描述 投票:0回答:1

我正在使用omniauth通过twitter验证用户身份。 omn​​iauth提供访问令牌。现在我想将获取或发布请求发送到twitter。我不想使用任何宝石。我想用net :: http做。

甚至在twitter api文档中!我无法为此找到一个好的教程

任何人都可以帮忙吗?谢谢

ruby ruby-on-rails-3 twitter
1个回答
2
投票

Here它正是你所需要的,所以,既然你已经获得了omniauth的令牌和秘密,那么现在你将使用它:

def prepare_access_token(oauth_token, oauth_token_secret)
  consumer = OAuth::Consumer.new("APIKey", "APISecret", { :site => "https://api.twitter.com", :request_token_path => '/oauth/request_token', :access_token_path => '/oauth/access_token', :authorize_path => '/oauth/authorize', :scheme => :header })
  token_hash = { :oauth_token => oauth_token, :oauth_token_secret => oauth_token_secret }  
  access_token = OAuth::AccessToken.from_hash(consumer, token_hash )
  access_token
end

然后你发了一条推文:

msg = {'status' => 'Hey look I can tweet via OAuth!'}
access_token = prepare_access_token(token, secret)
response = access_token.post('https://api.twitter.com/1/statuses/update.json', msg, { 'Accept' => 'application/xml' })

阅读链接上提供的文章以获取更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.