2012-07-20 54 views
0

因此,我使用Google登錄並使用代碼參數訪問了我的回調網址。OAuth2我如何處理來自Google的響應代碼?

以下是我如何啓動我的客戶端,我使用的是oauth2 gem。

def oauth_client(channel_name) 
    file = YAML.load_file("#{Rails.root}/config/oauth_credentials.yml") 
    client_id = file['oauth_credentials'][channel_name]['client_id'] 
    client_secret = file['oauth_credentials'][channel_name]['secret'] 
    site = file['oauth_credentials'][channel_name]['site'] 

    OAuth2::Client.new(client_id, client_secret, site: site, authorize_url: "/o/oauth2/auth", connection_opts: { params: { scope: "https://www.googleapis.com/auth/adsense https://www.googleapis.com/auth/analytics.readonly" } }) 
    end 

    def oauth_url_for(channel_name) 
    client = oauth_client(channel_name) 
    client.auth_code.authorize_url(:redirect_uri => oauth_callback_url(channel: channel_name)) 
    end 

這裏是我的控制器

class Oauth2Controller < ApplicationController 
    include ApplicationHelper 

    def callback 
    token = oauth_client(params[:channel]).auth_code.get_token(params[:code], :redirect_uri => oauth_callback_url(channel: params[:channel])) 
    current_user.connections.create!(channel: params[:channel], token: token) 
    render text: request.inspect 
    end 
end 

可惜我不能因爲get_token中從谷歌迴應說您請求的網頁無效。

回答