来自其他目录的 PyDrive client_secrets
PyDrive client_secrets from other directory
我正在使用 PyDrive 将文件从我使用 Pyinstaller 打包的桌面 Python 应用程序上传到 GoogleDrive。我想尽可能地隐藏 client_secrets.json 所以我将它嵌入到 Pyinstaller .exe 文件中,使用这个解决方案:Bundling Data files with PyInstaller 2.1 and MEIPASS error --onefile
但是,PyDrive 未从 Pyinstaller 放置数据文件的临时目录中找到 client_secrets 文件。
如何让 PyDrive 从另一个目录(尤其是 AppData)读取 json 文件?我正在考虑将文件从临时目录移动到工作目录,然后再进行身份验证和删除,但有些用户没有管理员权限,无法修改程序文件(安装应用程序的位置)
我看到我可以使用一个 settings.yaml 文件,我可以引用另一个目录,但是 pyinstaller 似乎使用 sys._MEIPASS 变量将嵌入的 client_secrets.json 放在临时文件夹中,所以我不知道它会在哪里。
我必须将那个有价值的东西直接传递给 GoogleAuth(),有没有办法做到这一点?
确实不是最好的解决方案,但我只是自己修改了 Pydrive auth.py 文件来完成...
我通过添加此方法来使用我用来添加有关 MEIPASS 的代码的技巧:
def resource_path(self,relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
并修改这些方法:
加载凭证文件的方法:(我不关心它是否出现,但你当然可以不将其设置为我的)
def LoadCredentialsFile(self, credentials_file=None):
"""Loads credentials or create empty credentials if it doesn't exist.
Loads credentials file from path in settings if not specified.
:param credentials_file: path of credentials file to read.
:type credentials_file: str.
:raises: InvalidConfigError, InvalidCredentialsError
"""
if credentials_file is None:
credentials_file = self.settings.get('save_credentials_file')
if credentials_file is None:
raise InvalidConfigError('Please specify credentials file to read')
try:
storage = Storage(self.resource_path(credentials_file))
self.credentials = storage.get()
except CredentialsFileSymbolicLinkError:
raise InvalidCredentialsError('Credentials file cannot be symbolic link')
加载client.secrets的方法:
def LoadClientConfigFile(self, client_config_file=None):
"""Loads client configuration file downloaded from APIs console.
Loads client config file from path in settings if not specified.
:param client_config_file: path of client config file to read.
:type client_config_file: str.
:raises: InvalidConfigError
"""
if client_config_file is None:
client_config_file = self.settings['client_config_file']
try:
client_type, client_info = clientsecrets.loadfile(self.resource_path(client_config_file))
...method continue here...
以及初始化方法:
def __init__(self, settings_file='settings.yaml',http_timeout=None):
"""Create an instance of GoogleAuth.
This constructor just sets the path of settings file.
It does not actually read the file.
:param settings_file: path of settings file. 'settings.yaml' by default.
:type settings_file: str.
"""
self.http_timeout=http_timeout
ApiAttributeMixin.__init__(self)
self.client_config = {}
try:
self.settings = LoadSettingsFile(self.resource_path(settings_file))
except SettingsError:
self.settings = self.DEFAULT_SETTINGS
else:
if self.settings is None:
self.settings = self.DEFAULT_SETTINGS
else:
ValidateSettings(self.settings)
我知道这可能不是一个好方法 :) 但如果有人知道更好的方法,但它有效...
所以如果有人有更好的解决方案,请告诉我:)
更简单的解决方案:
from pydrive.auth import GoogleAuth
GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = path_to_secrets_file
设置路径时,请确保路径中没有前导 /:
例如:
path_to_secrets_file = '/docs/secrets.json' -> 这将从根目录搜索并抱怨找不到文件。如果您省略 / 它会正确地与当前工作文件夹合并。
from pydrive.auth import GoogleAuth
GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = path_to_secrets_file
要更改默认设置文件,您需要在实例化 GAuth 实例时设置它。下面,我在工作目录中创建了一个名为 config_files 的子文件夹,并将 pydrive 设置 yaml 重命名如下:
from pydrive2.auth import GoogleAuth
gauth = GoogleAuth(settings_file='config_files/pydrive_settings.yaml')
我正在使用 PyDrive 将文件从我使用 Pyinstaller 打包的桌面 Python 应用程序上传到 GoogleDrive。我想尽可能地隐藏 client_secrets.json 所以我将它嵌入到 Pyinstaller .exe 文件中,使用这个解决方案:Bundling Data files with PyInstaller 2.1 and MEIPASS error --onefile
但是,PyDrive 未从 Pyinstaller 放置数据文件的临时目录中找到 client_secrets 文件。
如何让 PyDrive 从另一个目录(尤其是 AppData)读取 json 文件?我正在考虑将文件从临时目录移动到工作目录,然后再进行身份验证和删除,但有些用户没有管理员权限,无法修改程序文件(安装应用程序的位置)
我看到我可以使用一个 settings.yaml 文件,我可以引用另一个目录,但是 pyinstaller 似乎使用 sys._MEIPASS 变量将嵌入的 client_secrets.json 放在临时文件夹中,所以我不知道它会在哪里。
我必须将那个有价值的东西直接传递给 GoogleAuth(),有没有办法做到这一点?
确实不是最好的解决方案,但我只是自己修改了 Pydrive auth.py 文件来完成...
我通过添加此方法来使用我用来添加有关 MEIPASS 的代码的技巧:
def resource_path(self,relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
并修改这些方法:
加载凭证文件的方法:(我不关心它是否出现,但你当然可以不将其设置为我的)
def LoadCredentialsFile(self, credentials_file=None):
"""Loads credentials or create empty credentials if it doesn't exist.
Loads credentials file from path in settings if not specified.
:param credentials_file: path of credentials file to read.
:type credentials_file: str.
:raises: InvalidConfigError, InvalidCredentialsError
"""
if credentials_file is None:
credentials_file = self.settings.get('save_credentials_file')
if credentials_file is None:
raise InvalidConfigError('Please specify credentials file to read')
try:
storage = Storage(self.resource_path(credentials_file))
self.credentials = storage.get()
except CredentialsFileSymbolicLinkError:
raise InvalidCredentialsError('Credentials file cannot be symbolic link')
加载client.secrets的方法:
def LoadClientConfigFile(self, client_config_file=None):
"""Loads client configuration file downloaded from APIs console.
Loads client config file from path in settings if not specified.
:param client_config_file: path of client config file to read.
:type client_config_file: str.
:raises: InvalidConfigError
"""
if client_config_file is None:
client_config_file = self.settings['client_config_file']
try:
client_type, client_info = clientsecrets.loadfile(self.resource_path(client_config_file))
...method continue here...
以及初始化方法:
def __init__(self, settings_file='settings.yaml',http_timeout=None):
"""Create an instance of GoogleAuth.
This constructor just sets the path of settings file.
It does not actually read the file.
:param settings_file: path of settings file. 'settings.yaml' by default.
:type settings_file: str.
"""
self.http_timeout=http_timeout
ApiAttributeMixin.__init__(self)
self.client_config = {}
try:
self.settings = LoadSettingsFile(self.resource_path(settings_file))
except SettingsError:
self.settings = self.DEFAULT_SETTINGS
else:
if self.settings is None:
self.settings = self.DEFAULT_SETTINGS
else:
ValidateSettings(self.settings)
我知道这可能不是一个好方法 :) 但如果有人知道更好的方法,但它有效...
所以如果有人有更好的解决方案,请告诉我:)
更简单的解决方案:
from pydrive.auth import GoogleAuth
GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = path_to_secrets_file
设置路径时,请确保路径中没有前导 /:
例如: path_to_secrets_file = '/docs/secrets.json' -> 这将从根目录搜索并抱怨找不到文件。如果您省略 / 它会正确地与当前工作文件夹合并。
from pydrive.auth import GoogleAuth
GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = path_to_secrets_file
要更改默认设置文件,您需要在实例化 GAuth 实例时设置它。下面,我在工作目录中创建了一个名为 config_files 的子文件夹,并将 pydrive 设置 yaml 重命名如下:
from pydrive2.auth import GoogleAuth
gauth = GoogleAuth(settings_file='config_files/pydrive_settings.yaml')