【问题标题】:Get Gmail User Profile Picture and Name with Google API client使用 Google API 客户端获取 Gmail 用户个人资料图片和名称
【发布时间】:2020-10-15 14:42:15
【问题描述】:

Google API clientClass: Google::Apis::PeopleV1::NameGoogle::Apis::PeopleV1::Photo 我如何达到这些端点?我试过了

p = Google::Apis::PeopleV1::Photo.new
response = p.url

但它返回nil。与 GmailService 相对的有像 get_user_message 这样的实例方法,我可以这样调用

@service = Google::Apis::GmailV1::GmailService.new
# authorization is done
response = @service.get_user_message(user_id, message_id)

它返回一个具有messageresponse 对象,现在我想获取用户名和照片,但我没有找到任何方法来访问此信息。如何使用 API 客户端检索用户的姓名和照片?

【问题讨论】:

  • 虽然我不确定我是否能正确理解你的目标,但我提出了一个答案。你能确认一下吗?如果这不是您期望的方向,我深表歉意。
  • 我正在使用 Google API ruby​​ 客户端获取电子邮件。我想要实现的是在我的收件箱中获取发送和接收电子邮件的用户的姓名和头像。我只有他们的电子邮件地址,这可能吗?

标签: ruby google-api google-api-client google-api-ruby-client


【解决方案1】:

我认为在 Gmail API 中使用 users.messages.get 方法检索到的值不包括“用户个人资料图片”。 Ref 例如,为了获取用户头像的 URL,使用 People API 中的 people.connections.list 方法怎么样?示例脚本如下。

示例脚本:

service = Google::Apis::PeopleV1::PeopleServiceService.new
service.authorization = authorize
response = service.list_person_connections(
    'people/me',
    'page_size': 1000,
    'person_fields': "names,emailAddresses,photos"
)
puts "No connections" if response.connections.empty?
result = response.connections.map {|person| {
    'resourceName': person.resource_name,
    'email': person.email_addresses[0].value,
    'photos': person.photos.map {|e| e.url}
}}
puts result

结果:

上述脚本运行时,返回如下结果。

[
  {"resourceName": "people/###", "email": "###", "photos": ["https://###"]},
  {"resourceName": "people/###", "email": "###", "photos": ["https://###", "https://###"]},
  ,
  ,
  ,
]
  • phtots的网址是用户照片的网址。

注意:

  • 在这种情况下,otherContacts.list 的方法似乎没有返回用户的照片数据。

参考:

【讨论】:

  • people.connections.list 方法说它“提供经过身份验证的用户的联系人列表”。我的联系人中没有这些人,他们在我的 Gmail 帐户中发送/接收了电子邮件。这算不算在我的联系人中?
  • @Prime 感谢您的回复。我带来的不便表示歉意。在这种情况下,需要在人员 API 中使用“otherContacts”。 Ref 但不幸的是,otherContacts.list 的方法似乎并没有像上面提到的那样返回用户的照片数据。在这种情况下,Gmail API 也无法检索此类 photp。对此我深表歉意。
猜你喜欢
  • 2018-08-25
  • 2019-02-27
  • 1970-01-01
  • 2021-09-09
  • 1970-01-01
  • 2021-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多