我有一个应用程序需要使用Firebase服务帐户密钥。根据文档,它是一个短期密钥。那么,每次密钥过期时,我是否需要手动下载服务帐户密钥JSON文件?
1.这是我用来从JSON文件中提取令牌的代码。
SCOPES = ['https://www.googleapis.com/auth/firebase.messaging']
def _get_access_token():
credentials = ServiceAccountCredentials.from_json_keyfile_name(
'{valid_path/file.json}', SCOPES)
access_token_info = credentials.get_access_token()
return access_token_info.access_token
2.链接为:https://fcm.googleapis.com/v1/projects/{valid_project_id}/messages:send
3.Content-Type : application/json; UTF-8
4.Authorization : Bearer *access_token_returned_in_step_1*
5.正文
{
"message":{
"name":"name_field",
"android":{
"ttl":"81000s",
"data":{
"Name":"Z",
"Surname":"Z"
},
"notification":{
"title":"Hello",
"body":"World"
}
},
"topic":"test_topic"
}
}
6. 最初表现良好。 但是,随后...
{
"error":{
"code":401,
"message":"Request had invalid authentication credentials.
Expected OAuth 2 access token, login cookie or other valid
authentication credential. See
https://developers.google.com/identity/sign-in/web/devconsole-
project.",
"status":"UNAUTHENTICATED"
}
}
7. 当我再次下载服务帐户密钥JSON文件并运行相同的过程时,它可以正常工作。
因此,有没有不手动下载服务帐户密钥JSON文件的方法呢?