python - 如何获取用于 Firebase 云消息传递的 OAuth2.0 token

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

我已经设置我的网络应用程序来接收 Firebase 云消息,但为了发送它们,我知道我需要一个 OAuth2.0 token ,它是从我的服务帐户私钥生成的。我已经下载了 key 并安装了适用于 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()

关于python - 如何获取用于 Firebase 云消息传递的 OAuth2.0 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48291682/

相关文章:

python - 查看之前的命令是否仍在 PyCharm IPython 控制台中运行?

python - 如何处理具有多行字符串参数的嵌套函数调用的重新格式化

android - 如何在 android 的 firebase ui 身份验证中添加 Logo ?

android - 未调用 Firebase 自定义 token 身份验证回调

ios - 如何将 JSON 数据从登录 View 传递到多 View Controller ,是否可能?

Python - 从表中提取数据的爬虫

python - 在Python中使用pyodbc在sql server中使用内连接更新语句

javascript - Firebase google auth 未完全注销

ios - Instagram-API 集成认证

javascript - 将 SPA 中的 client_id 和 client_secret 发送到 auth 服务器有什么意义?