2012-04-24 27 views
0

我正在使用omniauth通過twitter驗證用戶身份。 omn​​iauth提供訪問令牌。現在我想發送獲取或發佈請求到twitter。我不想使用任何寶石。我想用net :: http來做。如何在Twitter上使用訪問令牌在rails上使用GET和POST

即使在twitter api文檔中!我無法找到一個很好的教程這

任何人都可以幫助嗎?感謝

回答

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) 
    return 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' }) 

閱讀提供的鏈接獲取更多信息的文章。

+0

謝謝你爲我工作 – santosh 2012-04-26 14:37:37

相關問題