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

时间:2019-04-24 09:39:25

标签: ruby-on-rails ruby

我不明白我做错了什么。我想向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_creat”:“创建于”:at 7400,:7200,   {“ access_token”:“ c6d69786f6075633:8647d6c6b6f6968327:232c92f977a301d033eec321c3d82b73bb65ebec33f9fcc8f6c2d7575c8b0d88”,“ token_type”:“ Bearer”,“ expires_in”:460在创建:46   追溯(最近一次通话):1:from Integration.rb:25:in   <main>' integration.rb:16:in授权':未定义的方法[[]'   nil:NilClass(NoMethodError)

  1. 为什么access_token生成两次?
  2. 为什么对nil:NilClass有未定义的方法'[]'?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

似乎该代码中有2个put出现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}