【问题标题】:How to get OAuth2.0 Token for Firebase Cloud Messaging如何获取 Firebase 云消息传递的 OAuth2.0 令牌
【发布时间】:2018-06-25 18:39:46
【问题描述】:

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

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


【解决方案1】:

这应该可以解决:

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

【讨论】:

  • @jiamo 你知道如何在 .Net 或 JavaScript 中做到这一点吗?
  • 有类似的 perl 解决方案吗?
猜你喜欢
  • 2022-08-19
  • 2021-02-18
  • 2016-11-21
  • 1970-01-01
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
  • 2022-12-03
相关资源
最近更新 更多