0

だから私はGoogleでログインし、コードパラメータでコールバックURLにヒットしました.

クライアントを開始する方法は次のとおりです。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

残念ながら、リクエストしたページが無効であるという Google からの応答により、get_token を実行できません。

4

1 に答える 1

1

token_urlクライアントの初期化またはそれ以降で値を定義しているようには見えません。「o/oauth2/token」です。デフォルトでは「/oauth/token」が使用されるため、「要求したページが無効です」というエラーの原因である可能性があります。

ソース: http://rubydoc.info/gems/oauth2/0.8.0/OAuth2/Client#initialize-instance_method

于 2012-07-25T00:23:36.957 に答える