JSON 密钥文件可以嵌入到 Python 文件中吗?

Can a JSON Keyfile be embedded in a Python file?

我有一个小型 Python 应用程序,它提供了一个 GUI 来使用 PysimpleGUI 更新 Google sheet。 我想使用 pyinstaller 将其转换为 EXE 文件,以便非技术用户能够轻松使用它。

该过程工作正常,但我需要将 JSON 密钥文件保留在 EXE 文件的“下一个”,以便 运行(JSON 为访问 Google sheet)。 是否可以将 JSON 嵌入到 python 文件中,以便它包含在转换过程中?

如果没有,是否有另一种方法可以使最终用户尽可能简单和万无一失?

这是代码的当前相关部分:

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)

正如@RJ Adriaansen 提到的那样,您可以创建一个字典来存储 json 密钥文件中的详细信息。您还可以使用 ServiceAccountCredentials.from_json_keyfile_dict 以便保留代码的结构。请参阅下面的示例:

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

参考:

样本: