使用intridea ruby gem的OAuth2[英] OAuth2 with intridea ruby gem

本文是小编为大家收集整理的关于使用intridea ruby gem的OAuth2的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有以下代码:

token = client.auth_code.get_token(code, :redirect_uri => 'http://localhost:3000')
response = token.get('https://api.foursquare.com/v2/users/self/checkins', {:mode => :query})

问题在于,无论如何:我指定的模式我总是在授权标题中获得持有人令牌.所讨论的代码是一个私有set_token,它始终取决于默认值:始终:header.

的模式.

我在使用错误吗?

谢谢!

推荐答案

似乎有一个问题OAuth2 Gem如何通过对象内部的变异性传递,因此模式和param_name似乎在途中丢失了.解决问题的方法是创建一个具有正确参数的新访问对象,而不是使用速记.此示例针对FourSquares API进行了测试.

require "oauth2"

client = OAuth2::Client.new(
  "CLIENT_ID",
  "CLIENT_SECRET", 
  :authorize_url => "/oauth2/authorize", 
  :token_url => "/oauth2/access_token", 
  :site => "https://foursquare.com/"
)

puts client.auth_code.authorize_url(:redirect_uri => "http://localhost:4000")

code = gets.chomp

token = client.auth_code.get_token(code, :redirect_uri => "http://localhost:4000")

token = OAuth2::AccessToken.new(client, token.token, {
  :mode => :query,
  :param_name => "oauth_token",
})

response = token.get('https://api.foursquare.com/v2/users/self/checkins')

puts response.body

本文地址:https://www.itbaoku.cn/post/2090854.html

问题描述

I have the following code:

token = client.auth_code.get_token(code, :redirect_uri => 'http://localhost:3000')
response = token.get('https://api.foursquare.com/v2/users/self/checkins', {:mode => :query})

The problem is that no matter what :mode I specify I always get a Bearer token in Authorization header. The code in question is a private set_token which always depends on the default :mode which is always :header.

Am I using it wrong?

Thanks!

推荐答案

There seems to be a problem how the oauth2 gem passes variabels inside the objects so mode and param_name seems to be lost on the way. A solution to the problem would be to create a new AccessToken object with the correct parameters instead of using the shorthand. This example is tested against Foursquares api and it works.

require "oauth2"

client = OAuth2::Client.new(
  "CLIENT_ID",
  "CLIENT_SECRET", 
  :authorize_url => "/oauth2/authorize", 
  :token_url => "/oauth2/access_token", 
  :site => "https://foursquare.com/"
)

puts client.auth_code.authorize_url(:redirect_uri => "http://localhost:4000")

code = gets.chomp

token = client.auth_code.get_token(code, :redirect_uri => "http://localhost:4000")

token = OAuth2::AccessToken.new(client, token.token, {
  :mode => :query,
  :param_name => "oauth_token",
})

response = token.get('https://api.foursquare.com/v2/users/self/checkins')

puts response.body