Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged

i wanted to add the updated version ( FCM APIs to HTTP v1 ) of fcm  api which is like this 

headers = {
"Authorization": "Bearer " + _get_access_token(),
"Content-Type": "application/json; UTF-8"
}
response = requests.post(
"https://fcm.googleapis.com/v1/projects/my_project_id/messages:send",
data=json.dumps(message),
headers=headers

here is code which i have included to get the access token 

def _get_access_token():
"""Retrieve a valid access token that can be used to authorize requests.
:return: Access token.
"""
credentials = service_account.Credentials.from_service_account_file(
'service_account_key.json', scopes=SCOPES)
request = google.auth.transport.requests.Request()
credentials.refresh(request)
return credentials.token

print(_get_access_token())
 I have added the service account json to same directory 
service_account_key.json
 directory look like this 
module
|-- models
|   |-- service_account_key.json
|-- |---get_token_from_key.py | |-- __init__.py |-- other_module_files..

 i,m getting this error 


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/odoo/modules/_service_account_info.py", line 78, in from_filename
    with io.open(filename, "r", encoding="utf-8") as json_file:
FileNotFoundError: [Errno 2] No such file or directory: 'service_account_key.json'


Avatar
Opusti
Avtor Best Answer

so by using the  following code 


from odoo.modules.module import get_module_resource


 Service_account_key= get_module_resource('mymodule', 'directory (like models or controllers or static/src/ ),  'filename(like in my case'service_account_key.json' )')


it works
or you can refer this for clearancehttps://www.odoo.com/forum/help-1/read-file-contents-on-module-location-156533

Avatar
Opusti
Related Posts Odgovori Prikazi Aktivnost
4
dec. 23
45671
1
jan. 23
3518
1
mar. 24
6311
0
jun. 22
2646
1
feb. 21
2680