【问题标题】:Obtaining both the name and phone number from a Google People API search从 Google People API 搜索中获取姓名和电话号码
【发布时间】:2021-12-29 00:22:59
【问题描述】:

我正在使用 Python 中的 Google People API 来查询我的 Google 联系人列表。我可以让这个功能工作,但一个特定的操作不起作用。

results = service.people().connections().list(
    resourceName='people/me',
    pageSize=1,
    personFields='names,phoneNumbers').execute()
connections = results.get('connections', [])

for person in connections:
    data = person.get('names', [])

这很有效——它给了我一个名字列表。但是,我想要一份姓名和电话号码的列表。如果我这样做:

for person in connections:
    data = person.get('phoneNumbers', [])

这也有效。我得到一个电话号码列表。但是,如果我尝试同时获得两者,我不会得到任何结果:

for person in connections:
    data = person.get('names,phoneNumbers', [])

根据我对 People API 文档 (https://developers.google.com/people/api/rest/v1/people/get) 的解释,这应该可行。相反,我什么也得不到。

我做错了什么?

【问题讨论】:

    标签: python google-api google-people-api


    【解决方案1】:

    当您调用person.get() 时,您不是调用v1/people/get 端点,您只是从字典 中获取值。

    之前对service.people().connections()....execute() 的调用已经从 API 中获取了所有信息并将其放入字典列表中。

    然后,当您遍历 connections 时,您会得到一个名为 person 的字典,其中包含两个键 namesphoneNumbers。 您的代码正在尝试获取名称为 names,phoneNumbers 的密钥,但该密钥不存在。

    不确定您如何使用 data 或您希望它是什么类型,但让 data 包含姓名和电话只需这样做:

    data = person
    

    或者直接在其余代码中使用person

    【讨论】:

      【解决方案2】:

      正如@marmor 所说,错误在于您访问响应内容的方式。 dict.get() 方法确实会同时拒绝两个键。 您可以使用以下两种不同的方法。

      # Calling them separately
      personName = person.get('names', [])
      personPhone = person.get('phoneNumbers',[])
      # Calling them together using list compression
      keys = ["names","phoneNumbers"]
      data = [person.get(key) for key in keys]
      

      您应该参考people().connections().list() 以更好地了解响应格式。在这种情况下,它是Person 类型的资源

      另一个可能的近似方法是,获取姓名及其电话号码的列表并将它们存储在字典中:

      代码示例
      def getNameAndPhones():
        service = people_service()
        results = service.people().connections().list(
          resourceName= "people/me",                                
          personFields='names,phoneNumbers').execute()              
        person_dict = {}           
        for per in  results['connections']:                         
          try:
            name =  per['names'][0]['displayName'] 
            # As the response is an array
            # we need to do some list comprehension
            phoneNumbers = [x['value'] for x in per['phoneNumbers']]         
            person_dict[name] = phoneNumber
          except : 
            name =  per['names'][0]['displayName']         
            person_dict[name] = "No phone Number"
      
        print(person_dict)
        return person_dict
      
      响应
      {
         "Eco":["00112233"],
         "Maxim maxim":"No phone Number",
         "dummy super":[
            "123123123",
            "12312309812390"
         ]
      }
      
      文档

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-23
        • 1970-01-01
        • 2021-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多