如何获取用于Firebase Cloud Messaging的OAuth2.0令牌



我已经将我的网络应用设置为接收 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

假设我缺少导入,但我不确定是什么。

这应该可以解决:

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

相关内容

  • 没有找到相关文章

最新更新