我们正在将一个大型企业应用程序从Google Contacts API迁移到People API。
以前,使用OAuth令牌向Contacts API发出请求很容易。我们将使用OAuth进行身份验证,然后将令牌传递给谷歌联系人API,如下所示:
# access_token is the token we receive from OAuth
@user = GoogleContactsApi::User.new(access_token)
# make a request
contact_objects = user.contactsPeopleService code展示了如何使用API key,然后只提到了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。如果你能提供这个具体的例子,那就太好了。
谢谢!?
发布于 2021-10-28 17:01:55
看起来我们需要访问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
endhttps://stackoverflow.com/questions/69757947
复制相似问题