首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用google- API -ruby-client People API传递OAuth令牌?

如何使用google- API -ruby-client People API传递OAuth令牌?
EN

Stack Overflow用户
提问于 2021-10-28 16:40:10
回答 1查看 62关注 0票数 0

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

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

代码语言:javascript
复制
# 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 key,然后只提到了OAuth令牌:

代码语言:javascript
复制
        #  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。如果你能提供这个具体的例子,那就太好了。

谢谢!?

EN

回答 1

Stack Overflow用户

发布于 2021-10-28 17:01:55

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

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69757947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档