有关使用刷新令牌生成访问令牌的查询



我可以使用下面提到的代码成功地将事件推送到Google日历中。下面的控制器代码每次尝试推送事件时都会刷新令牌。它工作正常。

 class Add2gcalendarController < ApplicationController
  before_filter :authenticate_user!   
  def push  
       event = {'summary' => 'Appointment','location' => 'Somewhere','start' => {'dateTime' => '2014-06-03T10:00:00.000-07:00'},'end' => {'dateTime' => '2014-06-03T10:25:00.000-07:00'}}
          client = Google::APIClient.new
          client.authorization.client_id =ENV["GOOGLE_KEY"]
          client.authorization.client_secret = ENV["GOOGLE_SECRET"]
          client.authorization.grant_type = 'refresh_token'
          client.authorization.refresh_token = saved_refresh_token
          client.authorization.fetch_access_token!  
          service = client.discovered_api('calendar', 'v3')                   
          @result = client.execute(
        :api_method => service.events.insert,
        :parameters => {'calendarId' => 'primary'},
        :body => JSON.dump(event), # where cal is the object containing at least "summary".
        :headers => {'Content-Type' => 'application/json'})
 end

我的问题:

  1. 每次尝试推送事件时刷新令牌是否是一种好习惯,无论令牌是否过期?

  2. 使用刷新令牌生成访问令牌是否有限制?

如果访问令牌未过期,则实际上没有理由强制刷新访问令牌。这样做对您的应用来说是低效的(用户必须等待另一个HTTP调用),并且极端(不断刷新或每次API调用)可能会导致Google出现5xx错误。

如果 Ruby on rails 库自动为您处理刷新,我建议让它完全为您处理刷新。这些库通常由了解OAuth处理最佳实践的Google员工编写。

相关内容

最新更新