我已经设置了我的网络应用以接收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
我认为我错过了导入,但我不确定是什么。
答案 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()