[英]How to pass OAuth token using google-api-ruby-client People API?
We are migrating a large enterprise application from the Google Contacts API to the People API.我们正在将一个大型企业应用程序从 Google Contacts API 迁移到 People API。
Before, using the OAuth token to make requests to the Contacts API was easy.以前,使用 OAuth 令牌向 Contacts API 发出请求很容易。 We would authenticate with OAuth, and then pass the token to the Google Contacts API like so:
我们将使用 OAuth 进行身份验证,然后将令牌传递给 Google Contacts API,如下所示:
# access_token is the token we receive from OAuth
@user = GoogleContactsApi::User.new(access_token)
# make a request
contact_objects = user.contacts
The PeopleService code shows how to use an API key, and then only makes mention of the OAuth token: PeopleService 代码展示了如何使用 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
But I've been unable to find an example of how to use the OAuth token to make requests to the People API using the Ruby gem.但是我一直无法找到一个示例,说明如何使用 OAuth 令牌使用 Ruby gem 向 People API 发出请求。
Could you please provide a simple example of how to make a People API request using the OAuth token?您能否提供一个简单的示例,说明如何使用 OAuth 令牌发出 People API 请求? Specifically, we want access to a user's contacts' email address and phone numbers.
具体来说,我们希望访问用户联系人的电子邮件地址和电话号码。 I believe we will be using get_people .
我相信我们会使用get_people 。 If you could provide this specific example, that would be wonderful.
如果你能提供这个具体的例子,那就太好了。
Thank you!谢谢! 😄
😄
It looks like we needed to access access_token.token
and set it like so:看起来我们需要访问
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.