【问题标题】:To make an API call with an oAuth token使用 oAuth 令牌进行 API 调用
【发布时间】:2015-07-09 22:15:16
【问题描述】:

我想在 Ruby 中发出 oAuth 请求。我浏览了一些示例,但没有一个使用oauth_token_secret and oauth_token 发出请求,他们只使用consumer_keyconsumer_secret 来获取oauth_token_secretoauth_token。但我已经有了oauth_token_secretoauth_token

例如,我尝试使用的这个

require 'rubygems'
require 'oauth'
consumer = OAuth::Consumer.new(consumer_key, consumer_secret,
                               {
                                 :site=> "https://www.google.com",
                                 :scheme=> :header,
                                 :http_method=> :post,
                                 :request_token_path => "/accounts/OAuthGetRequestToken",
                                 :access_token_path => "/accounts/OAuthGetAccessToken",
                                 :authorize_path=> "/accounts/OAuthAuthorizeToken",
                                 :signature_method=>"RSA-SHA1"},
                               # :private_key_file=>PATH_TO_PRIVATE_KEY
                               )

request_token = consumer.get_request_token()

puts "Visit the following URL, log in if you need to, and authorize the app"
puts request_token.authorize_url
puts "When you've authorized that token, enter the verifier code you are assigned:"

verifier = gets.strip

puts "Converting request token into access token..."

access_token=request_token.get_access_token(:oauth_verifier => verifier)

puts "access_token.token --> #{access_token.token}" # But I initially have it
puts "access_token.secret --> #{access_token.secret}" # But I initially have it

就我而言,有 4 个密钥:

consumer_key = "anonymous"
consumer_secret = "anonymous"
oauth_token_secret = "fdsfdsfdfdsfds"
oauth_token = "fdsfdsfdfdsfdsdsdsdsdsdsds"

所以我需要做的是,使用一些额外的 get 参数和 oAuth 令牌向某个 url 发出 API 请求并获得答案。

我如何在 Ruby 中做到这一点?

【问题讨论】:

    标签: ruby oauth


    【解决方案1】:
    #!/usr/bin/env ruby
    require 'rubygems'
    require 'oauth'
    require 'json'
    

    您需要获取您的 access_token (OAuth::AccessToken)。

    # Initialisation based on string values:
    consumer_key = 'AVff2raXvhMUxFnif06g'
    consumer_secret = 'u0zg77R1bQqbzutAusJYmTxqeUpWVt7U2TjWlzbVZkA'
    access_token = 'R1bQqbzYm0zg77tAusJzbVZkAVt7U2T'
    access_token_secret = 'sVbVZkAt7U2TjWlJYmTxqR1bQqbzutAuWzeUpu0zg77'
    
    @consumer = OAuth::Consumer.new(consumer_key, consumer_secret, {:site=>'http://my.site'})
    accesstoken = OAuth::AccessToken.new(@consumer, access_token, access_token_secret)
    

    一旦你有了OAuth::AccessToken 对象,你就可以这样做:

    json_response = accesstoken.get('/photos.xml')
    # or
    json_response = accesstoken.post(url, params_hash)
    

    等等

    响应是一个 json 对象。要阅读它,您可以这样做:

    response = JSON.parse(json_response.body)
    # which is a hash
    # you just access content like
    id = response["id"]
    

    【讨论】:

    • 我不需要 OAuth::AccessToken,因为我已经有了 oauth_secret 和 oauth_token。
    • @AlanDert 我知道。使用您在此处获得的对象:access_token=request_token.get_access_token(:oauth_verifier => verifier)
    • 什么是验证者?你能给出完整的例子吗?请注意,Typhoeus 有错误,我无法使用它。
    • 如何为 oAuth2 做到这一点?请再给我看一遍。我在GitHub上看到了例子,但是AccessToken的构造函数和第一个版本的不同。
    最近更新 更多