【问题标题】:How to pass OAuth token using google-api-ruby-client People API?如何使用 google-api-ruby-client People API 传递 OAuth 令牌?
【发布时间】:2021-12-13 21:12:18
【问题描述】:

我们正在将一个大型企业应用程序从 Google Contacts API 迁移到 People API。

以前,使用 OAuth 令牌向联系人 API 发出请求很容易。我们将使用 OAuth 进行身份验证,然后将令牌传递给 Google 联系人 API,如下所示:

# access_token is the token we receive from OAuth
@user = GoogleContactsApi::User.new(access_token)

# make a request
contact_objects = user.contacts

PeopleService code 展示了如何使用 API 密钥,然后只提到了 OAuth 令牌:

        #  API key. Your API key identifies your project and provides you with API access,
        #  quota, and reports. Required unless you provide an OAuth 2.0 token

但我一直找不到一个示例,说明如何使用 OAuth 令牌通过 Ruby gem 向 People API 发出请求。

能否提供一个简单示例,说明如何使用 OAuth 令牌发出 People API 请求?具体来说,我们想要访问用户联系人的电子邮件地址和电话号码。我相信我们会使用get_people。如果你能提供这个具体的例子,那就太好了。

谢谢! ????

【问题讨论】:

    标签: ruby-on-rails ruby oauth-2.0 google-people-api


    【解决方案1】:

    看起来我们需要访问access_token.token 并像这样设置它:

    require 'google/apis/people_v1'
    
    class GooglePeopleApiWrapper
      attr_reader :service, :access_token
    
      def initialize(oauth_object)
        # outh_object is the `access_token` from my question, which is 
        # provided in the OAuth response
        @oauth_object = oauth_object 
        @service = Google::Apis::PeopleV1::PeopleServiceService.new
        @service.authorization = @oauth_object.token
      end
    
      def fetch_contacts
        # Fetch the next 10 events for the user
        contact_objects = @service.list_person_connections(
          'people/me',
          page_size: 10,
          person_fields: 'names,emailAddresses,phoneNumbers',
        )
        
        etc...
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      • 2011-11-21
      相关资源
      最近更新 更多