如何为Firebase云消息传递获取OAuth2.0令牌

时间:2018-01-16 23:55:40

标签: python firebase oauth-2.0 firebase-cloud-messaging

我已经设置了我的网络应用以接收Firebase云消息,但为了发送它们,我了解到我需要一个OAuth2.0令牌,它是从我的服务帐户私钥生成的。我已下载密钥并安装了适用于Python的Google API客户端库。这是我的代码:

from oauth2client import *
def _get_access_token():
  """Retrieve a valid access token that can be used to authorize requests.

  :return: Access token.
  """
  credentials = ServiceAccountCredentials.from_json_keyfile_name(
      'service-account.json', FCM_SCOPE)
  access_token_info = credentials.get_access_token()
  return access_token_info.access_token

_get_access_token()

当我跑步时,我得到了

Traceback (most recent call last):
  File "get-oauth-token.py", line 11, in <module>
    _get_access_token()
  File "get-oauth-token.py", line 7, in _get_access_token
    credentials = ServiceAccountCredentials.from_json_keyfile_name(
NameError: name 'ServiceAccountCredentials' is not defined

我认为我错过了导入,但我不确定是什么。

1 个答案:

答案 0 :(得分:4)

这应解决:

from oauth2client.service_account import ServiceAccountCredentials
fsm_scope = 'https://www.googleapis.com/auth/firebase.messaging'

def _get_access_token():
"""Retrieve a valid access token that can be used to authorize requests.

:return: Access token.
"""
credentials = ServiceAccountCredentials.from_json_keyfile_name(
    'service-account.json', fsm_scope)
access_token_info = credentials.get_access_token()
return access_token_info.access_token

_get_access_token()