2012-06-22 1 views
1

다음은 사용자가 내 앱이 OAuth를 통해 Google 캘린더에 액세스하도록 허용하도록 허용하는 코드입니다. 나는 이것을 this sample code에 근거한다.Google 캘린더 API를 사용하려고 할 때 "인증 코드 누락"오류가 발생했습니다.

그것은 대부분의 시간을 작동하지만, 때로는 services 컨트롤러에서 create_google_calendar 행동에 client.authorization.fetch_access_token! 라인에 ArgumentError: Missing authorization code 오류가 있습니다. 해당 줄을 주석으로 처리하면 client.authorization 속성은 모두 null입니다.

저는 Rails 3.2.0과 Ruby 1.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 

답변

1

진실은, 나도 몰라. 귀하의 코드는 내게 적합합니다. 하지만 적어도 오류의 의미를 말해 줄 수는 있습니다. 누락 된 인증 코드는 액세스 토큰을 가져올 때 "인증 코드"부여 유형을 수행하려고한다고 생각한다는 의미입니다. 사용자로부터 권한을 얻은 후 첫 번째 패스에서 수행하는 것과 달리 실제로 새로 고침 토큰에서 액세스 토큰을 얻으려는 경우 인증 개체를 올바르게 설정하지 않았을 수 있습니다.

client.authorization.grant_type 값을 검사하여이를 확인할 수 있습니다. 가장 최근 버전의 클라이언트에서는 grant_type 값을 수동으로 설정하여 특정 모드를 강제 적용 할 수 있습니다. 실제 문제의 내용에 따라 더 많은 정보 오류 메시지가 표시 될 수 있습니다.