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

Python 如何获取Firebase云消息传递的OAuth2.0令牌,python,firebase,oauth-2.0,firebase-cloud-messaging,Python,Firebase,Oauth 2.0,Firebase Cloud Messaging,我已将我的web应用程序设置为接收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. :re

我已将我的web应用程序设置为接收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
回溯(最近一次呼叫最后一次):
文件“GetOAuth token.py”,第11行,在
_获取\u访问\u令牌()
文件“get-oauth token.py”,第7行,在\u-get\u-access\u-token中
credentials=ServiceAccountCredentials.from_json_keyfile_name(
NameError:未定义名称“ServiceAccountCredentials”
我假设缺少导入,但我不确定是什么原因。

这应该可以解决:

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

从oauth2client.service\u帐户导入ServiceAccountCredentials
但是
FCM\u范围
我没有know@jiamo您知道如何在.Net或JavaScript中实现这一点吗?有类似的perl解决方案吗?