简体   繁体   中英

Faraday not working on Rails

I'm trying to use Faraday with Rails 4 for Google API Oauth in below code:

class GoogleApisController < ApplicationController

    def oauth2callback
        require 'faraday'
        render layout: false

        conn = Faraday.new(:url => 'https://accounts.google.com',:ssl => {:verify => false}) do |faraday|
            faraday.request  :url_encoded
            faraday.response :logger
            faraday.adapter  Faraday.default_adapter
        end

        @result = conn.post '/o/oauth2/token', {'code' => params[:code],
            'client_id' => "some_id",
            'client_secret' => "some_secret",
            'redirect_uri' => "some_redirect_uri",
            'grant_type' => 'authorization_code'}

    end
end

But when I do @result.body.inspect it doesn't return anything.

So @result return nil and doesn't seem to process at all.

However when I try the above codes in liveshell it seems to work.

Is there a reason why this is happening?

If you try to render before you instantiate the object you are trying to render , the template won't actually have anything in it. Moving the render statement to the bottom of the method will take care of the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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