首页 文章

Gmail API推送通知提供错误403

提问于
浏览
2

我正在尝试获取有关我在电子邮件中收到的所有新收件箱邮件的推送通知 . 我已经配置了pubsub doc中提到的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(最近一次调用最后一次):文件“/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中返回包装(* args,** kwargs)文件“/ usr / local / lib /python2.7/dist-packages/googleapiclient/http.py“,第723行,执行中引发HttpError(resp,content,uri = self.uri)googleapiclient.errors.HttpError:https://www.googleapis.com/ gmail / v1 / users / me / watch?alt = json返回“无效的topicName与项目不匹配/ SOME_ANOTHER_PROJECT / topics / *”>

1 回答

  • 1

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

    主题名称可以是您在项目下选择的任何名称(即匹配项目/ myproject / topics / *,其中myproject是Google Developers Console中为您的项目列出的项目ID) .

相关问题