簡體   English   中英

Gmail API 推送通知給出錯誤 403

[英]Gmail API Push Notifications gives Error 403

我正在嘗試獲取有關我在電子郵件中收到的所有新收件箱消息的推送通知。 我已經按照 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()

當我運行上述程序時,遇到以下錯誤:

回溯(最近一次調用):文件“/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/*">不匹配

根據另一個答案,您似乎只能為屬於您自己項目的主題創建監視,而不能為另一個項目創建監視。 文檔似乎支持這一點,因為它意味着您必須指定myproject (您自己的項目 ID):

主題名稱可以是您在項目下選擇的任何名稱(即匹配 projects/myproject/topics/*,其中 myproject 是在 Google Developers Console 中為您的項目列出的項目 ID)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM