rails google-api-客户联系方式api

rails google-api-client contact api

我想访问具有 google 个联系人 API 的用户的联系人列表。 我已经设法获得令牌和刷新令牌,现在我正尝试在我的 rails 服务器上使用 then。 google-api-client gem 似乎是要走的路,但我找不到要使用的 discovered_api。 Greg Baugues 提供了一个很棒的 tuto 让 gmail API 正常工作。一般要求好像是

client = Google::APIClient.new
client.authorization.access_token = user_token
service = client.discovered_api('gmail')
result = client.execute(
  :api_method => service.users.labels.list,
  :parameters => {'userId' => 'me'},
  :headers => {'Content-Type' => 'application/json'})
pp JSON.parse(result.body)

但我找不到如何查询联系人。 运行

client.discovered_apis.each do |gapi|
  puts "#{gapi.title} \t #{gapi.id} \t #{gapi.preferred} \n"
end

(来自 here)现在显示 API 与联系人相关,我想知道这是否在 gem...[=20= 的 alpha 版本中实现]

Google Contacts API is on Google's older GData API standard and is not supported by the discovery API. There is a pretty extensive guide for plain Ruby and a helper gem. Retrieving all contacts 没有提供 Ruby 示例,但 Python 示例应该很容易翻译。

def PrintAllContacts(gd_client):
  feed = gd_client.GetContacts()
  for i, entry in enumerate(feed.entry):
    print '\n%s %s' % (i+1, entry.name.full_name.text)
    if entry.content:
      print '    %s' % (entry.content.text)
    # Display the primary email address for the contact.
    for email in entry.email:
      if email.primary and email.primary == 'true':
        print '    %s' % (email.address)

如@abraham 所述,发现 API 不支持 Google 联系人 API。这是我在 ruby 中使用 gem google_contacts_apioauth2 从访问令牌中做到的(感谢 Rael Gugelmin Cunha 将它们指向我):

client = OAuth2::Client.new(client_id, client_secret, site: url)
token = OAuth2::AccessToken.new(client, access_token)
google_contacts_user = GoogleContactsApi::User.new(token)
contacts = google_contacts_user.contacts

可能有一些更优雅的方法来做到这一点,但这很有效:)