我很努力地訪問Google Contacts API。首先我嘗試了google-api-ruby-client gem
,但事實證明它不支持Contacts API
。如何在ruby中訪問Google Contacts API
下一張照片是google_contacts_api gem。我使用oauth2
來訪問驗證密鑰(Getting authentication token guide question)。但是,將令牌正確傳遞給API後,會產生錯誤。
`<main>': undefined method `[]' for #<GoogleContactsApi::GroupSet:0x000000039fcad8>` (NoMethodError).
這是我的代碼。
# get token using oauth2 gem, and use it below in the google_contacts_api.
google_contacts_user = GoogleContactsApi::User.new(token)
contacts = google_contacts_user.contacts
groups = google_contacts_user.groups
# group methods
group = groups[0]
group.contacts
puts group.contacts
# contact methods
puts contacts.count
puts groups.count
contact = contacts[0]
contact.primary_email
contact.emails
我在做什麼錯?
UPDATE:
由於@alvin
建議現在運轉。但小組聯繫人沒有被打印出來。相反,它是印刷#<GoogleContactsApi::ContactSet:0x000000020e49d8>
。實例:
#<GoogleContactsApi::ContactSet:0x000000020e49d8>
#<GoogleContactsApi::ContactSet:0x0000000504aec0>
#<GoogleContactsApi::ContactSet:0x0000000518dfd0>
#<GoogleContactsApi::ContactSet:0x000000052d9290>
#<GoogleContactsApi::ContactSet:0x000000054280d8>
#<GoogleContactsApi::ContactSet:0x0000000558c2f8>
#<GoogleContactsApi::ContactSet:0x00000005746eb8>
#<GoogleContactsApi::ContactSet:0x000000058a3ea0>
我怎樣才能打印組聯繫人:這裏是什麼是這個代碼
groups = google_contacts_user.groups
# group methods
groups.each do |group|
group_contacts = group.contacts
puts group_contacts
end
輸出打印?