简体   繁体   English

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

[英]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.

相关问题 如何授权 google-api-ruby-client? - How to authorize the google-api-ruby-client? 我如何通过google-api-ruby-client 0.9授权OAuth 2.0使用Google API服务? - How can I OAuth 2.0 authorise a Google API service with google-api-ruby-client 0.9? Ruby:使用 google-api-ruby-client SDK 将文件上传到 Google Drive 失败并出现 Timeout::Error - Ruby : File upload to Google drive using google-api-ruby-client SDK failing with Timeout::Error 尝试使用google-api-ruby-client更新CurrentLocation的Google纬度会返回“ 400无效值” - Trying to update Google Latitude of CurrentLocation using google-api-ruby-client returns “400 Invalid Value” 需要在Rails中序列化或加载一次Google的API(google-api-ruby-client) - Need to serialize, or load once, the Google's API (google-api-ruby-client) in Rails google-api-ruby-client设置错误的事件开始和结束时间 - google-api-ruby-client setting wrong event start and end time 使用google-api-ruby-client gem有redirect_uri_mismatch错误 - Use google-api-ruby-client gem have redirect_uri_mismatch error 结合使用Google OAuth2 API和Ruby on Rails - Using Google OAuth2 API with Ruby on Rails 使用Ruby的Google文档API的OAuth问题(Rails 3.0.6) - OAuth issues with Google Docs API using Ruby (Rails 3.0.6) 将Yahoo API与Oauth红宝石一起使用 - Using yahoo api with Oauth ruby
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM