【问题标题】:AttributeError: 'Resource' object has no attribute 'messages'AttributeError:“资源”对象没有属性“消息”
【发布时间】:2018-10-04 21:26:18
【问题描述】:

我正在努力解决 Google Gmail API(python) 的一个奇怪问题。 我正在尝试发送消息,凭据看起来正确(如果我允许应用程序管理电子邮件等,我将被重定向到站点以接受)

范围:'https://mail.google.com/'

代码:

import httplib2
from apiclient import discovery
def SendMessage(user_id, message):
  """Send an email message.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    message: Message to be sent.

  Returns:
    Sent Message.
  """
  from GoogleCredentialsMail import get_credentials
  credentials = get_credentials()
  http = credentials.authorize(httplib2.Http())
  service = discovery.build('admin', 'directory_v1', http=http)

  message = (service.users().messages().send(userId=user_id,        
   body=message).execute())
  print('Message Id: %s' % message['id'])
  return(message)

但我得到一个错误:

AttributeError: 'Resource' object has no attribute 'messages'

有什么建议吗?

API 链接:https://developers.google.com/gmail/api/guides/sending

【问题讨论】:

  • 您查看过Python Quickstart Tutorial 吗?正如我在那里看到的,您必须传递的服务名称是 gmail 和版本 v1 例如。

标签: python gmail-api


【解决方案1】:

当我使用管理目录(构建机器人以在 Google API 中移动)时,我习惯了使用:

service = discovery.build('admin', 'directory_v1', http=http)

感谢 Igle,我从一开始就开始重新思考,发现了这个棘手的错误。 我现在可以使用它了,它看起来:

service = discovery.build('gmail', 'v1', http=http)

【讨论】:

    猜你喜欢
    • 2017-07-22
    • 2017-09-27
    • 2019-02-16
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 2021-04-19
    相关资源
    最近更新 更多