Home > Blockchain >  Can a JSON Keyfile be embedded in a Python file?
Can a JSON Keyfile be embedded in a Python file?

Time:05-31

I have a small Python app that provides a GUI to update a Google sheet using PysimpleGUI. I would like to convert it to an EXE file using pyinstaller so that the non-technical user will be able to make use of it easily.

The process works fine, but I need to keep the JSON key file "next" to the EXE file for it to run (the JSON provides the authentication key for access to the Google sheet). Is it possible to embed the JSON in the python file so it gets included in the conversion process?

If not, is there another way to make this as simple and fool proof as possible for the end user?

This is the current relevant part of the code:

import gspread
import pandas as pd
from oauth2client.service_account import ServiceAccountCredentials
import PySimpleGUI as sg

# define the scope
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']


# add credentials to the account
creds = ServiceAccountCredentials.from_json_keyfile_name('ABCDEFG.json', scope)

# authorize the clientsheet 
client = gspread.authorize(creds)

CodePudding user response:

As what @RJ Adriaansen mentioned, you can create a dictionary that stores the details from your json keyfile. You can also use ServiceAccountCredentials.from_json_keyfile_dict so you can retain the structure of your code. See sample below:

creds = ServiceAccountCredentials.from_json_keyfile_dict({
    'client_email': xxx,
    'private_key': xxx,
    'type': xxx,
    'client_id': xxx,
    'private_key_id': xxx
}, scope)

Reference:

Samples:

  • Related