我正在尝试通过Python访问Gmail API。这是指令:https://developers.google.com/gmail/api/quickstart/python。 我已经复制了certificate.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文件。但是它们位于同一文件夹中。我也尝试过以管理员身份运行代码,但没有成功。
任何帮助将不胜感激。 谢谢
答案 0 :(得分:1)
我不确定您发生了什么,但是我使用PyCharm或Visual Studio Code运行脚本,并且脚本运行良好。
唯一的前提条件(可能是您跳过了这一步)是,您首先应该生成credentials.json文件,下载该文件并将其放入具有python脚本的目录中。另外,我创建了一个空的tokens.json文件。
首先,它将自动打开默认浏览器,以指示您要指定的Gmail帐户。接下来,它将输出您的Gmail帐户的标签。
答案 1 :(得分:0)
如果与脚本位于同一位置,则可以尝试:
import os
path = os.path.abspath('credentials.json')
with open(path,'r'):
//code