粘贴并使用oauth2访问令牌

时间:2014-11-19 13:58:25

标签: ruby oauth-2.0 sinatra

我有一个使用ruby和sinatra的应用程序。我希望能够粘贴这种格式的令牌:

{"access_token":"token...uwD--Of0ikNjr8AeW3oS9zP2rith3fdsf2Wk","token_type":"Bearer","expires_in":3600,"created":1415727521}

进入文本区域并使用它。现在的问题是,粘贴的令牌是字符串格式。我已经尝试将其转换为哈希和json格式,但它还没有工作。 这是我的代码:

post '/tokenize' do
  got_token = params[:token] # the pasted token
  token_hash = JSON.parse(got_token) #I am not sure if I did this correctly.but it is producing an hash.
  token = token_hash.to_json # producing the token in json format as the original token that I pasted.
  get_contacts(JSON.parse(token)) # calling the function that should use the token. giving an error since it is not a valid oauth token.
 redirect to ("/tokenize/finish")
end

错误:它产生错误,因为令牌不是有效的oauth令牌。 有人请告诉我如何使令牌有效和可用。

1 个答案:

答案 0 :(得分:0)

您可以使用params手动实例化令牌类OAuth2 :: AccessToken,您已经收集了该构造的完整定义find here

但最终您需要使用OAuth2客户端来手动实例化。

例如:

client = OAuth2::Client.new('client_id', 'client_secret', :site => 'https://example.org')

token = OAuth2::AccessToken.new(client, collected_token, collected_opts)

response = token.get('/api/resource', :params => { 'query_foo' => 'bar' })