rails api如何在请求中填充标头

问题描述 投票:0回答:2

我不明白我做错了什么。我想向API发送一个简单的请求,但它不起作用:

    class Paytrace
      require 'rest-client'

      attr_reader :auth_token, :authorize

      def initialize()
        @auth_token = auth_token
      end

      def auth_token
        response = RestClient.post 'https://api.paytrace.com/oauth/token', { grant_type: :password, username: "loginname", password: "htmlkoi8r" }
        puts response
      end

      def authorize
        headers = {:Authorization => "Bearer #{auth_token['access_token']}"}

        response1 = RestClient.get('https://api.paytrace.com/v1/transactions/sale/keyed', headers)

        puts response1
      end
    end

    a = Paytrace.new
    a.authorize

console.log

lucker @ lucker-pc:〜/ git / paytrace-testh $ ruby​​ integration.rb {“access_token”:“c6d69786f6075633:8647d6c6b6f6968327:092e8cfc553726d2b8198577ea2836f41173aae68a53aa1d2af2b2c7f65dcdc7”,“token_type”:“Bearer”,“expires_in”:7200,“created_at”:1556098344 } {“access_token”:“c6d69786f6075633:8647d6c6b6f6968327:232c92f977a301d033eec321c3d82b73bb65ebec33f9fcc8f6c2b2565ebec33f9fc8f6202602c8b00b0d88”,“token_type”:“Bearer”,“expires_in”:7200,“created_at”:1556098346}回溯(最近一次呼叫最后一次):1:来自integration.rb:25 :in <main>' integration.rb:16:inauthorize':undefined method` []'for nil:NilClass(NoMethodError)

  1. 为什么access_token生成了两次?
  2. 为什么nil有一个未定义的方法'[]':NilClass?
ruby-on-rails ruby
2个回答
1
投票

你的方法auth_token没有返回response,但nilputs返回nil)。

顺便说一句,你不需要attr_reader :authorize,因为你有一个具有该名称的方法。

此外,当您设置attr_reader :auth_token时,方法auth_token必须重命名(并且可能成为private)。

将您的代码更改为:

    class Paytrace
      require 'rest-client'

      attr_reader :auth_token

      def initialize()
        @auth_token = get_auth_token
      end      

      def authorize
        headers = {:Authorization => "Bearer #{auth_token['access_token']}"}

        RestClient.get('https://api.paytrace.com/v1/transactions/sale/keyed', headers)
      end

      private

        def get_auth_token
          RestClient.post 'https://api.paytrace.com/oauth/token', { grant_type: :password, username: "loginname", password: "htmlkoi8r" }        
        end
    end

    a = Paytrace.new
    puts a.auth_token
    puts a.authorize

0
投票

看起来这个代码中有3个错误。第12和第20行

headers = {:Authorization => "Bearer #{auth_token['access_token']}"}

应该

headers = {:Authorization => "Bearer #{auth_token[:access_token]}"}

要么

headers = {:Authorization => "Bearer #{@auth_token[:access_token]}"}

试试这段代码

    class Paytrace
      require 'rest-client'

      attr_reader :auth_token, :authorize

      def initialize()
        @auth_token = auth_token
      end

      def auth_token
        response = RestClient.post 'https://api.paytrace.com/oauth/token', { grant_type: :password, username: "loginname", password: "htmlkoi8r" }
        # puts response
      end

      def authorize
        headers = {:Authorization => "Bearer #{@auth_token[:access_token]}"}

        response1 = RestClient.get('https://api.paytrace.com/v1/transactions/sale/keyed', headers)

        # puts response1
      end
    end

    a = Paytrace.new
    a.authorize

如果你检查是,请注意你的响应哈希

{:access_token=>"c6d69786f6075633:8647d6c6b6f6968327:092e8cfc553726d2b8198577ea2836f41173aae68a53aa1d2af2b2c7f65dcdc7",
   :token_type=>"Bearer",
   :expires_in=>7200,
   :created_at=>1556098344}

并不是

{"access_token":"c6d69786f6075633:8647d6c6b6f6968327:092e8cfc553726d2b8198577ea2836f41173aae68a53aa1d2af2b2c7f65dcdc7","token_type":"Bearer","expires_in":7200,"created_at":1556098344} 
© www.soinside.com 2019 - 2024. All rights reserved.