How do I authorize a service account for Google Calendar API in Ruby?

后端 未结 2 2093
我在风中等你
我在风中等你 2021-01-28 13:37

How do I authorize a service account for Google Calendar API in Ruby? I tried the quick start guide, but it crashed.

https://developers.google.com/calendar/quickstart/ru

相关标签:
2条回答
  • 2021-01-28 14:18

    OK I found a way.

    https://developers.google.com/api-client-library/ruby/auth/service-accounts

    require 'google/apis/calendar_v3'
    require 'googleauth'
    
    # Get the environment configured authorization
    scopes =  ['https://www.googleapis.com/auth/calendar']
    authorization = Google::Auth.get_application_default(scopes)
    
    # Clone and set the subject
    auth_client = authorization.dup
    auth_client.sub = 'myemail@mydomain.com'
    auth_client.fetch_access_token!
    
    # Initialize the API
    service = Google::Apis::CalendarV3::CalendarService.new
    service.authorization = auth_client
    
    # Fetch the next 10 events for the user
    calendar_id = 'primary'
    response = service.list_events(calendar_id,
                                   max_results: 10,
                                   single_events: true,
                                   order_by: 'startTime',
                                   time_min: Time.now.iso8601)
    puts 'Upcoming events:'
    puts 'No upcoming events found' if response.items.empty?
    response.items.each do |event|
      start = event.start.date || event.start.date_time
      puts "- #{event.summary} (#{start})"
    end
    

    And

    >set GOOGLE_APPLICATION_CREDENTIALS=client_secrets.json
    
    C:\Users\Chloe\workspace>ruby quickstart.rb
    Upcoming events:
    - Test (2018-05-17)
    - SSL Certificate for CMS (2019-02-13)
    

    But I wonder where it saves the refresh token and access token? All I have to do now is make it work for ephemeral file systems like Heroku and store the tokens in the database.

    0 讨论(0)
  • 2021-01-28 14:34

    For anyone still looking this is what worked for me:

    require 'google/apis/calendar_v3'
    require 'googleauth'
    
    scope = 'https://www.googleapis.com/auth/calendar'
    
    authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
      json_key_io: File.open('/path/to/creds.json'),
      scope: scope)
    
    authorizer.fetch_access_token!
    
    service = Google::Apis::CalendarV3::CalendarService.new
    service.authorization = authorizer
    
    calendar_id = 'primary'
    response = service.list_events(calendar_id,
                                   max_results: 10,
                                   single_events: true,
                                   order_by: 'startTime',
                                   time_min: Time.now.iso8601)
    puts 'Upcoming events:'
    puts 'No upcoming events found' if response.items.empty?
    response.items.each do |event|
      start = event.start.date || event.start.date_time
      puts "- #{event.summary} (#{start})"
    end
    

    The trick was finding the docs for the google-auth-library-ruby https://github.com/googleapis/google-auth-library-ruby

    0 讨论(0)
提交回复
热议问题