簡體   English   中英

rails api如何在請求中填充標題

[英]rails api how to fill headers in request

我不明白我做錯了什么。 我想向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:092e8cfc553726d2b8198577ea2836f41173aae68a53aa1d2af2b2c7f65dcear”:“”“”“”:“” } {“ access_token”:“ c6d69786f6075633:8647d6c6b6f6968327:232c92f977a301d033eec321c3d82b73bb65ebec33f9fcc8f6c2d7575c8b0d88”,“ token_type”:“ Bearer”,最近調用:“”,從最近開始:“”,“:”,“:”,“:”,“:”,“:”,“:”,“:”,“:”,“:”,“:”,“:”,“,”:“,”:“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”“” :in <main>' integration.rb:16:in authorize':nil:NilClass的未定義方法“ []”(NoMethodError)

  1. 為什么access_token生成兩次?
  2. 為什么為nil:NilClass提供了未定義的方法'[]'?

您的方法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

似乎該代碼2放入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} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM