简体   繁体   English

Gmail API 推送通知给出错误 403

[英]Gmail API Push Notifications gives Error 403

I am trying to get push notifications regarding all the new inbox messages I receive in my email.我正在尝试获取有关我在电子邮件中收到的所有新收件箱消息的推送通知。 I have configured the pubsub client as mentioned in the pubsub doc.我已经按照 pubsub 文档中的说明配置了 pubsub 客户端。 Following is the code:以下是代码:

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()

When I run the above Program, I encounter the following error:当我运行上述程序时,遇到以下错误:

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/*">回溯(最近一次调用):文件“/home/kmittal/workspace/engine/workers/newPubSubClient.py”,第 82 行,在 service.users().watch(userId='me', body=request).execute () 文件“/usr/local/lib/python2.7/dist-packages/oauth2client/util.py”,第 135 行,在 positional_wrapper 中返回wrapped(*args, **kwargs) 文件“/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 返回“无效的主题名称与项目/SOME_ANOTHER_PROJECT/topics/*">不匹配

As per this other answer , it looks like you can only create a watch for topics that are part of your own project, not another project.根据另一个答案,您似乎只能为属于您自己项目的主题创建监视,而不能为另一个项目创建监视。 Thedocumentation seems to back this up as it implies you must specify myproject (your own project ID):文档似乎支持这一点,因为它意味着您必须指定myproject (您自己的项目 ID):

The topic name can be any name you choose under your project (ie matching projects/myproject/topics/*, where myproject is the Project ID listed for your project in the Google Developers Console).主题名称可以是您在项目下选择的任何名称(即匹配 projects/myproject/topics/*,其中 myproject 是在 Google Developers Console 中为您的项目列出的项目 ID)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM