简体   繁体   English

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

[英]rails api how to fill headers in request

I don't understand what I do wrong. 我不明白我做错了什么。 I want to send a simple request to an API, and it didn't work: 我想向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 的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:232c92f977a301d033eec321c3d82b73bb65ebec33f9fcc8f6c2d7575c8b0d88","token_type":"Bearer","expires_in":7200,"created_at":1556098346} Traceback (most recent call last): 1: from integration.rb:25:in <main>' integration.rb:16:in authorize': undefined method `[]' for nil:NilClass (NoMethodError) lucker @ lucker-pc:〜/ git / paytrace-testh $ ruby​​ integration.rb {“ access_token”:“ c6d69786f6075633:8647d6c6b6f6968327:092e8cfc553726d2b8198577ea2836f41173aae68a53aa1d2af2b2c7f65dcear”:“”“”“”:“” } {“ access_token”:“ c6d69786f6075633:8647d6c6b6f6968327:232c92f977a301d033eec321c3d82b73bb65ebec33f9fcc8f6c2d7575c8b0d88”,“ token_type”:“ Bearer”,最近调用:“”,从最近开始:“”,“:”,“:”,“:”,“:”,“:”,“:”,“:”,“:”,“:”,“:”,“:”,“,”:“,”:“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”“” :in <main>' integration.rb:16:in authorize':nil:NilClass的未定义方法“ []”(NoMethodError)

  1. Why is the access_token generated twice? 为什么access_token生成两次?
  2. Why is there an undefined method '[]' for nil:NilClass? 为什么为nil:NilClass提供了未定义的方法'[]'?

Your method auth_token is not returning a response , but a nil ( puts returns nil ). 您的方法auth_token不返回response ,而是nilputs返回nil )。

Btw, you don't need attr_reader :authorize since you have a method with that name. 顺便说一句,您不需要attr_reader :authorize因为您有一个使用该名称的方法。

Also, as you are setting attr_reader :auth_token , the method auth_token must be rename (and maybe become private ). 此外,如要设置attr_reader :auth_token ,该方法auth_token必须重命名(可能成为private )。

Change your code to: 将您的代码更改为:

    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

Seems like there are 3 mistakes in this code the 2 puts. 似乎该代码2放入3个错误。 line 12 and 20 第12和20行

and the

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

should be 应该

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

or 要么

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

try this code 试试这个代码

    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

take care the your response hash if you check is 如果检查是,请注意您的响应哈希

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

and not 并不是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM