【问题标题】:Python - Gmail API - Instance of 'Resource' has no 'users' memberPython - Gmail API - “资源”实例没有“用户”成员
【发布时间】:2018-11-29 14:13:29
【问题描述】:

[编辑] 我现在已经在我的 linux 发行版上尝试了 Python 快速入门指南,它按预期工作,这让我相信这是一个仅限 Windows 的问题。 [end_edit]

遵循此处的 Python 快速入门指南:https://developers.google.com/gmail/api/quickstart/python

我遇到的问题是在使用 Python 时,

service = build('gmail','v1', http=creds.authorize(Http()))

下一行,即:

results = service.users().labels().list(userID='me').execute()

我收到此错误:

Instance of 'Resource' has no 'users' member

现在我已经尝试了 .NET 快速入门指南,并且效果很好。由于某种原因,API 无法在 python 上运行。

我的 google-api-python-client 的 pip 安装说它是最新的。

我正在使用 googleapiclient 而不是 apiclient,因为据我阅读,apiclient 仅出于遗留原因而存在,并且在此代码中使用它会导致错误“无法从 apiclient 导入构建”。

这是我的pipfreeze

快速入门代码:

"""
Shows basic usage of the Gmail API.

Lists the user's Gmail labels.
"""
from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

# Setup the Gmail API
SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=creds.authorize(Http()))

# Call the Gmail API
results = service.users().labels().list(userId='me').execute()
labels = results.get('labels', [])
if not labels:
    print('No labels found.')
else:
    print('Labels:')
    for label in labels:
        print(label['name'])

【问题讨论】:

  • 如果您在调试器中暂停,您可以检查service 变量的动态属性吗?如果发现步骤正常工作,那么您应该会看到各种根级别的 Gmail API 资源(例如 users())。如果它们都不存在,考虑到您声明从 linux 机器上运行相同的代码没有问题,我会怀疑是网络/防火墙或缓存问题。
  • “print(dir(service))”和“print(type(service))”显示什么?

标签: python google-api google-oauth gmail-api google-api-python-client


【解决方案1】:

应该是pylint问题。 您可以通过在违规语句后添加注释“# pylint: disable=maybe-no-member”来禁用特定行的 pylint 警告。

Hide some maybe-no-member Pylint errors

http://pylint.pycqa.org/en/latest/#is-it-possible-to-locally-disable-a-particular-message

【讨论】:

  • 我认为 OP 的问题与 Pylint 没有任何关系。问题是出现运行时错误,而不是 Pylint 错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 2020-05-05
  • 2019-05-27
  • 2020-12-04
  • 1970-01-01
  • 2021-05-24
相关资源
最近更新 更多