2016-08-03 87 views
2

我需要在沒有用戶身份驗證的情況下在我的日曆中插入事件。Google Calendar API插入事件,無需身份驗證Rails

變量:

OOB_URI = 'urn:ietf:wg:oauth:2.0:oob' 
    APPLICATION_NAME = 'Google Calendar API Ruby Quickstart' 
    CLIENT_SECRETS_PATH = './lib/client_secret.json' 
    CREDENTIALS_PATH = File.join(Dir.home, '.credentials', 
           "calendar-ruby-quickstart.yaml") 
    SCOPE = Google::Apis::CalendarV3::AUTH_CALENDAR 

    def initialize(meeting_data) 
    @meeting_data = meeting_data 
    end 

授權過程:

def authorize 
    FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH)) 

    client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH) 
    token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH) 
    authorizer = Google::Auth::UserAuthorizer.new(
     client_id, SCOPE, token_store) 
    user_id = '2' 
    credentials = authorizer.get_credentials(user_id) 
    if credentials.nil? 
     url = authorizer.get_authorization_url(
      base_url: OOB_URI) 
     puts "Open the following URL in the browser and enter the " + 
       "resulting code after authorization" 
     puts url 
     code = gets 
     credentials = authorizer.get_and_store_credentials_from_code(
      user_id: user_id, code: code, base_url: OOB_URI) 
    end 
    credentials 
    end 

創建方法:

def create_meeting 
    # Initialize the API 
    service = Google::Apis::CalendarV3::CalendarService.new 
    service.client_options.application_name = APPLICATION_NAME 
    service.authorization = authorize 
    ... 
    # Create event end post 
    ... 
    end 

當我開始這個方法,我必須作出一些控制檯技巧(我坐令牌從谷歌和過去他們到控制檯)和事件發佈後。我可以自動執行此過程嗎?或者採用壽命很長的令牌?

該過程必須在沒有用戶的情況下工作。

回答