【问题标题】:What do I do with my Google Oauth2 token when trying to call the Calendar API?尝试调用日历 API 时如何处理我的 Google Oauth2 令牌?
【发布时间】:2020-05-09 04:40:15
【问题描述】:

我正在尝试使用 Ruby 与 Google Calendar API 对话。我关注this guide 以获得用户的同意,并保存了他们的授权令牌和刷新令牌。我正在关注this guide 来调用实际的 API,但我在任何地方都找不到任何关于具体如何处理令牌的说明。我假设我应该将它包含在 API 请求中的某个地方,但是在哪里?

之前有人使用 Google 日历和 Oauth 吗?

【问题讨论】:

  • 应该更新问题以包含所需的行为、特定问题或错误以及重现问题所需的最短代码。

标签: oauth-2.0 google-api google-calendar-api google-oauth


【解决方案1】:

我从来没有使用过这个 API,也没有使用过 Ruby。

但也许这是您需要阅读的文档的一部分。 https://developers.google.com/identity/protocols/OAuth2InstalledApp#callinganapi

您在 Http 请求中添加标头。 在 Java 中(对不起,我真的对 Ruby 一无所知)我做了一些看起来像这样的事情:

headers.add("Authorization", "Bearer " + token);

希望对你有帮助

【讨论】:

    【解决方案2】:

    您需要关注Google Calendar API Quickstart,它具有触发认证流程的功能(def authorize)

    • 此函数将创建一个包含访问令牌和刷新令牌的令牌文件
    • 它会在必要时自动刷新令牌并授权您的日历服务
    • 获得授权服务后,您可以使用它来执行对 API 的任何调用

    示例如何将quickstart 与event creation 的指南互连:

    # This part is from the quickstart and includeds token creation and authorization flow
    require "google/apis/calendar_v3"
    require "googleauth"
    require "googleauth/stores/file_token_store"
    require "date"
    require "fileutils"
    
    OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze
    APPLICATION_NAME = "Google Calendar API Ruby Quickstart".freeze
    CREDENTIALS_PATH = "credentials.json".freeze
    # The file token.yaml stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    TOKEN_PATH = "token.yaml".freeze
    SCOPE = Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY
    
    ##
    # Ensure valid credentials, either by restoring from the saved credentials
    # files or intitiating an OAuth2 authorization. If authorization is required,
    # the user's default browser will be launched to approve the request.
    #
    # @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
    def authorize
      client_id = Google::Auth::ClientId.from_file CREDENTIALS_PATH
      token_store = Google::Auth::Stores::FileTokenStore.new file: TOKEN_PATH
      authorizer = Google::Auth::UserAuthorizer.new client_id, SCOPE, token_store
      user_id = "default"
      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:\n" + url
        code = gets
        credentials = authorizer.get_and_store_credentials_from_code(
          user_id: user_id, code: code, base_url: OOB_URI
        )
      end
      credentials
    end
    
    # Initialize the API
    service = Google::Apis::CalendarV3::CalendarService.new
    service.client_options.application_name = APPLICATION_NAME
    service.authorization = authorize
    require "google/apis/calendar_v3"
    require "googleauth"
    require "googleauth/stores/file_token_store"
    require "date"
    require "fileutils"
    
    OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze
    APPLICATION_NAME = "Google Calendar API Ruby Quickstart".freeze
    CREDENTIALS_PATH = "credentials.json".freeze
    # The file token.yaml stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    TOKEN_PATH = "token.yaml".freeze
    SCOPE = Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY
    
    ##
    # Ensure valid credentials, either by restoring from the saved credentials
    # files or intitiating an OAuth2 authorization. If authorization is required,
    # the user's default browser will be launched to approve the request.
    #
    # @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
    def authorize
      client_id = Google::Auth::ClientId.from_file CREDENTIALS_PATH
      token_store = Google::Auth::Stores::FileTokenStore.new file: TOKEN_PATH
      authorizer = Google::Auth::UserAuthorizer.new client_id, SCOPE, token_store
      user_id = "default"
      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:\n" + url
        code = gets
        credentials = authorizer.get_and_store_credentials_from_code(
          user_id: user_id, code: code, base_url: OOB_URI
        )
      end
      credentials
    end
    
    # Initialize the API
    service = Google::Apis::CalendarV3::CalendarService.new
    service.client_options.application_name = APPLICATION_NAME
    service.authorization = authorize
    
    # this part uses service to create an event
    
    event = Google::Apis::CalendarV3::Event.new(
      summary: 'Google I/O 2015',
      location: '800 Howard St., San Francisco, CA 94103',
      description: 'A chance to hear more about Google\'s developer products.',
      start: Google::Apis::CalendarV3::EventDateTime.new(
        date_time: '2015-05-28T09:00:00-07:00',
        time_zone: 'America/Los_Angeles'
      ),
      end: Google::Apis::CalendarV3::EventDateTime.new(
        date_time: '2015-05-28T17:00:00-07:00',
        time_zone: 'America/Los_Angeles'
      ),
      recurrence: [
        'RRULE:FREQ=DAILY;COUNT=2'
      ],
      attendees: [
        Google::Apis::CalendarV3::EventAttendee.new(
          email: 'lpage@example.com'
        ),
        Google::Apis::CalendarV3::EventAttendee.new(
          email: 'sbrin@example.com'
        )
      ],
      reminders: Google::Apis::CalendarV3::Event::Reminders.new(
        use_default: false,
        overrides: [
          Google::Apis::CalendarV3::EventReminder.new(
            reminder_method: 'email',
            minutes: 24 * 60
          ),
          Google::Apis::CalendarV3::EventReminder.new(
            reminder_method: 'popup',
            minutes: 10
          )
        ]
      )
    )
    
    result = service.insert_event('primary', event)
    puts "Event created: #{result.html_link}"
    
    

    链接这两个代码部分所需的只是更改名称 从client 到service,反之亦然。

    【讨论】:

      猜你喜欢
      • 2013-05-02
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 2017-09-29
      相关资源
      最近更新 更多