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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top