以下是我用来允许用户允许用户授权我的应用程序通过 OAuth 访问他们的 Google 日历的代码。我基于 this sample code .
大部分时间都有效,但有时 中的
操作。如果我注释掉该行,所有 client.authorization.fetch_access_token!
行会出现 ArgumentError: Missing authorization code
错误services
Controller 中的 create_google_calendarclient.authorization
属性都是 null
。
我正在使用 Rails 3.2.0 和 Ruby 1.9.2。
这是什么原因造成的?
gem 文件
gem 'google-api-client', :require => 'google/api_client'
服务.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
服务 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
最佳答案
事实上,我不知道。你的代码对我来说很合适。但我至少可以告诉你错误的含义。缺少授权代码意味着它认为您在获取访问 token 时正在尝试执行“授权代码”授权类型。如果您实际上是在尝试从刷新 token 中获取访问 token ,而不是在获得用户授权后的第一遍就这样做,那么您可能没有正确设置授权对象。
您可以通过检查 client.authorization.grant_type
值来检查这一点。在客户端的最新版本中,您可以手动设置 grant_type
值以强制执行特定模式,这可能会为您提供更多信息性错误消息,具体取决于实际问题。
关于ruby-on-rails - 尝试使用 Google Calendar API 时出现 "Missing authorization code"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11150167/
相关文章:
regex - 基于预定义文本字符串的复杂正则表达式来阻止色情
python - 使用 python 需求文件,您可以控制安装包依赖项的顺序吗?
python - 在 PyGame (SDL) 中使用 Cairo 时的字节顺序
python - 如何在 python smtplib 模块中设置主机地址?
c# - 当本地服务帐户在服务中运行时,并非有关 Process.GetProcesses 中进程的
.net - IBM.Data.DB2.DB2Exception : ERROR [08001] [
ruby-on-rails - 将 Rails 应用程序连接到 Microsoft Access