2012-07-20 2 views
0

Donc, je me suis connecté avec Google et il frappe mon URL de rappel avec un paramètre de code.OAuth2 que faire avec le code de réponse de Google?

Voici comment je lance mon client, j'utilise la gemme oauth2.

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 

Voici mon contrôleur

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 

Malheureusement, je ne peux pas get_token en raison d'une réponse de Google disant que la page demandée est invalide.

Répondre

Questions connexes