I have a json file and would like to put it in the enviroment variable, however, every time that I run my code I get an error.
Since I want to keep the credentials in the enviroment variable, I put it in an .env file. Now, what I need is to use this credential stored in the enviroment variable in the code.
It is stored like this: creds= '{ "type": "somethinghere", "project_id": "somethinghere", "private_key_id": "somethinghere", "private_key": "somethinghere" }'
The code I am running contains an script to write to google sheets:
load_dotenv()
API_Key = os.getenv("API_Key")
creds = os.getenv("creds")
json_creds = json.loads(creds)
class DataWriter:
def __init__(self, filename: str, sheetname: str) -> None:
self.filename = filename
self.sheetname = sheetname
self.scopes = [
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive'
]
self.credentials = ServiceAccountCredentials.from_json_keyfile_name(
json_creds, self.scopes)
# access the json key you downloaded earlier
# authenticate the JSON key with gspread
def write(self, data) -> None:
file = gspread.authorize(self.credentials)
sheet = file.open(self.filename).worksheet(self.sheetname)
return set_with_dataframe(sheet, data, include_index=False,
include_column_header=True, resize=False)
I am getting the following error: JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Would you be able to assist? The reason I want to do it is because I need to use Github actions. I am open to the best practice.
Thanks