python - Gmail API 推送通知给出错误 403

标签 python push-notification gmail-api google-cloud-pubsub

我正在尝试获取有关我在电子邮件中收到的所有新收件箱消息的推送通知。我已经按照 pubsub 文档中所述配置了 pubsub 客户端。以下是代码:

import httplib2

from apiclient import discovery
from oauth2client import client as oauth2client
from lib.common.models.main.users import User

from oauth2client.client import SignedJwtAssertionCredentials,\
    AccessTokenRefreshError

PUBSUB_SCOPES = ['https://www.googleapis.com/auth/pubsub']

def create_pubsub_client(http=None):
    credentials = oauth2client.GoogleCredentials.get_application_default()
    if credentials.create_scoped_required():
        credentials = credentials.create_scoped(PUBSUB_SCOPES)
    if not http:
        http = httplib2.Http()
    credentials.authorize(http)

    return discovery.build('pubsub', 'v1', http=http)

pubsub = create_pubsub_client()

topic = 'projects/<project-name>/topics/<Topic-name>'
policy = {
  'policy': {
    'bindings': [{
      'role': 'roles/pubsub.publisher',
      'members': ['serviceAccount:<my-service-account>'],
    }],
  }
}
resp = pubsub.projects().topics().setIamPolicy(resource=topic, body=policy).execute()

request = {
  'labelIds': ['INBOX'],
  'topicName': 'projects/<project-name>/topics/<Topic-name>'
}

f = file('link-to.p12', 'rb')
key = f.read()
f.close()

credentials = SignedJwtAssertionCredentials(
    'service-account-email',
    key,
    scope='https://mail.google.com/',
)

user = User.query.filter(User.id == 143).first()
accesskey = user.get_access_key(True)
credentials.access_token = accesskey['key']

service = discovery.build('gmail','v1',credentials=credentials)
service.users().watch(userId='me', body=request).execute()

当我运行上面的程序时,遇到以下错误:

Traceback (most recent call last): File "/home/kmittal/workspace/engine/workers/newPubSubClient.py", line 82, in service.users().watch(userId='me', body=request).execute() File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 135, in positional_wrapper return wrapped(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/googleapiclient/http.py", line 723, in execute raise HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: https://www.googleapis.com/gmail/v1/users/me/watch?alt=json returned "Invalid topicName does not match projects/SOME_ANOTHER_PROJECT/topics/*">

最佳答案

根据这个other answer , 看起来您只能为属于您自己的项目而不是另一个项目的主题创建监视。 documentation似乎支持这一点,因为它暗示您必须指定 myproject(您自己的项目 ID):

The topic name can be any name you choose under your project (i.e. matching projects/myproject/topics/*, where myproject is the Project ID listed for your project in the Google Developers Console).

关于python - Gmail API 推送通知给出错误 403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33916696/

相关文章:

gmail-api - GMail API 电子邮件退回

javascript - 尝试访问 gmail api 时出现无效范围错误

python - Celery 任务子进程填满并发槽?

python - 在 matplotlib 中 show() 之后保留无花果的内容?

ios - 在 ios7 中更新推送通知的角标(Badge)计数

iphone - 了解应用程序是否有权实时接收推送通知

javascript - 无法在 'waitUntil' 上执行 'ExtendableEvent'

Python 索引元组

python - 使用 opencv python 找到该形状的边缘点

javascript - 使用 Gmail 应用脚本按月对标签计数进行分组