3

以下は、ユーザーがOAuthを介して自分のアプリにアクセスすることをユーザーに許可できるようにするために使用しているコードです。このサンプルコードに基づいています。

ほとんどの場合は機能しますが、コントローラーのアクションの行にArgumentError: Missing authorization codeエラーがある場合があります。その行をコメントアウトすると、すべての属性はです。client.authorization.fetch_access_token!create_google_calendarservicesclient.authorizationnull

Rails3.2.0とRuby1.9.2を使用しています。

これを引き起こしているのは何ですか?

Gemfile

gem 'google-api-client', :require => 'google/api_client'

service.rb

def self.google_calendar_client google_calendar_service=nil
    client = Google::APIClient.new
    client.authorization.client_id = xxx
    client.authorization.client_secret = xxx
    client.authorization.scope = 'https://www.googleapis.com/auth/calendar'
    url_prefix = Rails.env.production? ? xxx : 'http://localhost:3000'
    client.authorization.redirect_uri = "#{url_prefix}/create_google_calendar"
    if google_calendar_service.present?
        client.authorization.update_token! :access_token => google_calendar_service.token, :refresh_token => google_calendar_service.google_calendar_refresh_token, :expires_in => google_calendar_service.google_calendar_expires_in, :issued_at => Time.at(google_calendar_service.google_calendar_issued_at)
        client.authorization.fetch_access_token! if client.authorization.expired?
    end

    client
end

services_controller.rb

def connect_google_calendar
    @google_calendar_url = Service.google_calendar_client.authorization.authorization_uri.to_s
end

def create_google_calendar
    client = Service.google_calendar_client
    client.authorization.code = params[:code]
    client.authorization.fetch_access_token!
    current_user.services.create :provider => 'google_calendar', :token => client.authorization.access_token, :google_calendar_refresh_token => client.authorization.refresh_token, :google_calendar_expires_in => client.authorization.expires_in, :google_calendar_issued_at => client.authorization.issued_at
end
4

1 に答える 1

2

真実は、私にはわかりません。あなたのコードは私には正しく見えます。しかし、少なくともエラーの意味を伝えることができます。認証コードが欠落しているということは、アクセス トークンをフェッチするときに、「認証コード」付与タイプを実行しようとしていると見なされることを意味します。ユーザーから認可を取得した後の最初のパスでアクセス トークンを取得するのではなく、実際にリフレッシュ トークンからアクセス トークンを取得しようとしている場合は、認可オブジェクトを正しく設定していない可能性があります。

これは、値を調べることで確認できclient.authorization.grant_typeます。クライアントのごく最近のバージョンでは、grant_type値を手動で設定して特定のモードを強制することができます。これにより、実際の問題によっては、より有益なエラー メッセージが表示される場合があります。

于 2012-06-23T04:06:50.333 に答える