【问题标题】:Where to keep your client_secret.json on windows machine?在 Windows 机器上保存 client_secret.json 的位置?
【发布时间】:2019-04-21 20:31:48
【问题描述】:

我正在使用 OAuth,用于登录 Gmail 帐户并通过 python 脚本发送电子邮件。 我已经下载了 client_secret.json 文件,该文件存储在 C:\Users\anuj.masand\ (主文件夹)中。 运行 Python 脚本时出现以下错误:

oauth2client.clientsecrets.InvalidClientSecretsError: ('Error opening file', 'client_secret.json', 'No such file or directory', 2)

我可以看到该文件在脚本需要的地方不可用。 我已经阅读了 clientsecrets.py 文件并知道以下代码会加载该文件。

def _loadfile(filename):
try:
    with open(filename, 'r') as fp:
        obj = json.load(fp)
except IOError as exc:
    raise InvalidClientSecretsError('Error opening file', exc.filename,
                                    exc.strerror, exc.errno)
return _validate_clientsecrets(obj)

我的代码直接跳到异常部分。 我的问题是在哪里存储 client_secret.json 文件?以便解释器可以找到文件并继续前进。 python 真正在哪里寻找这个文件?

参考:Script

【问题讨论】:

    标签: python-3.x oauth-2.0 gmail-api


    【解决方案1】:

    根据quickstart guide,您将下载的文件移动到您的工作目录。使用名为credentials.json 的文件,指南将文件读取实现为:

    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    store = file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('gmail', 'v1', http=creds.authorize(Http()))
    

    确保文件名完全匹配。

    【讨论】:

    • 感谢@jacque 的回复和您的时间,我已经想通了。我没有传递文件名,而是传递了完整路径(你保存文件的地方),它起作用了。我的问题的答案仍然存在。还是谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 2016-02-29
    相关资源
    最近更新 更多