【问题标题】:Cannot access token.json: No such file or directory Python无法访问 token.json:没有这样的文件或目录 Python
【发布时间】:2018-08-02 02:43:50
【问题描述】:

我正在尝试通过 Python 访问 Gmail API。 Here is the instruction。 我已经复制了credentials.json(我还重命名了它的副本并将其命名为token.json 并将其添加到主文件夹中)和主文件夹中的quickstart.py 文件。我在 cmd 中运行 quickstart.py 并使用 Atom IDE,但我得到:

    C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\_helpers.py:255: UserWarning: Cannot access token.json: No such file or directory  
     warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
    Traceback (most recent call last):

  File "C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\clientsecrets.py", line 121, in _loadfile
    with open(filename, 'r') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'credentials.json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\HP Pavilion\Documents\Atom\Gmail\quickstart.py", line 34, in <module>
    main()
  File "C:\Users\HP Pavilion\Documents\Atom\Gmail\quickstart.py", line 18, in main
    flow = client.flow_from_clientsecrets("credentials.json", SCOPES)
  File "C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\client.py", line 2135, in flow_from_clientsecrets
    cache=cache)
  File "C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\clientsecrets.py", line 165, in loadfile
    return _loadfile(filename)
  File "C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\clientsecrets.py", line 125, in _loadfile
    exc.strerror, exc.errno)
oauth2client.clientsecrets.InvalidClientSecretsError: ('Error opening file', 'credentials.json', 'No such file or directory', 2)

似乎找不到.json 文件。但是,它们位于相同的文件夹中。我也尝试以管理员身份运行代码,但没有成功。

【问题讨论】:

    标签: python gmail


    【解决方案1】:

    我不确定你发生了什么,但我使用 PyCharm 或 VS Code 运行脚本并且它运行完美。

    唯一的先决条件(可能您跳过了这一步)是您应该首先生成credentials.json 文件,下载它并将其放入您拥有python 脚本的同一目录中。另外,我创建了一个空的tokens.json 文件。

    首先,它会自动打开您的默认浏览器以指明您要指定的 Gmail 帐户。接下来,它将输出您的 Gmail 帐户的标签。

    【讨论】:

      【解决方案2】:

      我的错误是我右键单击了我的 JSON 文件,选择了重命名,然后将其命名为 credentials.JSON,这使得真正的文件名为 credentials.JSON.JSON。

      【讨论】:

      • 这可能是一条评论。
      【解决方案3】:

      如果和脚本在同一个位置,可以试试:

      import os
      path = os.path.abspath('credentials.json')
      with open(path,'r'):
        //code
      

      【讨论】:

        猜你喜欢
        • 2016-02-26
        • 1970-01-01
        • 2020-08-18
        • 2019-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-15
        • 2014-01-16
        相关资源
        最近更新 更多