代码之家  ›  专栏  ›  技术社区  ›  Romu

使用OAuth2 gem时发生代理错误

  •  0
  • Romu  · 技术社区  · 11 年前

    嗨,我正在尝试使用oauth 2将我的应用程序连接到本地API。 授权请求成功,并返回我的回调地址上的代码,以发出访问令牌请求。 这是我的回调代码:

    def client
      OAuth2::Client.new(KEY, SECRET, site: SITE, authorize_url: '/oauth2/auth', token_url: 'http://localhost:8080/oauth2/token')
    end
    
    get '/auth/callback' do
      access_token = client.auth_code.get_token(params[:code], "redirect_uri" => redirect_uri)
      access_token =  session[:access_token] = access_token.token
      @access_token = access_token
    end
    

    在get_token函数中,我检查了我的连接配置(正确):

    1.9.3 (#<OAuth2::Client:0x007fb255183e18>):0 > connection
    => #<Faraday::Connection:0x007fb2551817f8 
    @parallel_manager=nil, 
    @headers={"User-Agent"=>"Faraday v0.9.0"}, 
    @params={}, @options=#<Faraday::RequestOptions (empty)>, 
    @ssl=#<Faraday::SSLOptions (empty)>, 
    @default_parallel_manager=nil, 
    @builder=#<Faraday::RackBuilder:0x007fb255188760 
    @handlers=[Faraday::Request::UrlEncoded, Faraday::Adapter::NetHttp, Faraday::Response::Logger]>, 
    @url_prefix=#<URI::HTTP:0x007fb255187b08 URL:http://localhost:3000/>, 
    @proxy=#<Faraday::ProxyOptions uri=#<URI::HTTP:0x007fb255186b68 URL:http://proxy_url:port>, user="me", password=« MyPassword">>
    
    1.9.3 (#<OAuth2::Client:0x007fb255183e18>):0 > url
    => "http://localhost:8080/oauth2/token »
    
    1.9.3 (#<OAuth2::Client:0x007f9bdb94cce8>):0 > verb
    => :post
    
    1.9.3 (#<OAuth2::Client:0x007f9bdb94cce8>):0 > opts
    => {
          :body => {
            "client_id" => "Test",
            "client_secret" => "test_secret",
            "code" => "d3c4661b11ea5e2752852db72e1ce100f2f1804d",
            "grant_type" => "authorization_code",
            "redirect_uri" => "http://localhost:4567/auth/callback"
        },
          :headers => {
            "Content-Type" => "application/x-www-form-urlencoded"
        },
          :parse => nil,
          :raise_errors => true
    }
    

    来自请求的调试信息:

    I, [2014-08-20T12:01:19.450941 #12792]  INFO -- : post    http://localhost:8080/oauth2/token
    D, [2014-08-20T12:01:19.451040 #12792] DEBUG -- request: User-Agent: "Faraday v0.9.0"
    Content-Type: "application/x-www-form-urlencoded"
    I, [2014-08-20T12:01:19.451203 #12792]  INFO -- Status: 401
    D, [2014-08-20T12:01:19.451258 #12792] DEBUG -- response: cache-control: "no-cache"
    pragma: "no-cache"
    www-authenticate: "NTLM, BASIC realm=\"OUR_DOMAIN_NAME\""
    content-type: "text/html; charset=utf-8"
    proxy-connection: "close"
    set-cookie: "BCSI-CS-925062b0249dae29=2; Path=/"
    connection: "close"
    content-length: "706"
    x-rbt-optimized-by: "riv-olo (RiOS 8.5.1) IK"
    OAuth2::Error - Nice page from my proxy telling me that I’m not authentified on the domain
    

    当我使用授权代码向POSTMAN REST客户端发出POST请求时,一切都很好,API会返回一个access_token。 我不明白为什么请求会通过代理,因为我的所有应用程序和API都在我的本地主机上。

    有人能告诉我为什么使用我的本地应用程序追加此错误吗?

    谢谢

    2 回复  |  直到 11 年前
        1
  •  1
  •   Romu    11 年前

    由于我的邮递员请求有效,而不是我的应用程序,我使用Wireshark来查看它们之间的区别。 在Oauth中,默认使用我的代理参数。所以我的请求通过了代理IP。 在邮差上,请求直接发送到本地主机。 因此,我更改了客户端定义以强制空代理参数:

    PROXY_URI = ''
    PROXY_USER = ''
    PROXY_PASSWORD = ''
    
    def client
      OAuth2::Client.new(KEY, SECRET, site: SITE, authorize_url: '/oauth2/auth', token_url: 'http://localhost:8080/oauth2/token', connection_opts: {
        :proxy => {:uri => PROXY_URI,
                   :user => PROXY_USER,
                   :password => PROXY_PASSWORD}})
    end
    

    现在,请求按预期发送到localhost。 这对于本地测试是可以的,但现在我需要对代理变量进行条件赋值。

    为什么GET方法被发送到localhost而不是POST方法?这仍然是个谜!

        2
  •  0
  •   Sam Starling    11 年前

    听起来好像您试图通过代理访问本地主机,但这不起作用。如果您使用的是OS X:

    1. 系统首选项>网络 .
    2. 在左侧选择您正在使用的连接(例如“Wi-Fi”或“以太网”)。
    3. 单击右下角的“高级…”。
    4. 转到“代理”选项卡。
    5. 在底部的框中(“绕过代理设置”),添加 127.0.0.1, localhost .
    6. 单击“确定”,然后单击“应用”。

    完成后,OSX不应该通过代理将请求路由到本地主机,Postman应该可以正常工作。

    推荐文章