Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access token.json: No such file or directory Python

Tags:

python

gmail

I'm trying to access the Gmail API via Python. Here is the instruction. I have copied the credentials.json (I have also renamed a copy of it and called it token.json and added it into the main folder) and the quickstart.py files in the main folder. I run the quickstart.py in the cmd and using Atom IDE, but I get:

    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)

Seems like the .json files can't be found. They are however in the same folders. I have also tried to run the code as an administrator but without any success.

like image 218
AKJ Avatar asked Sep 02 '25 10:09

AKJ


2 Answers

I am not sure what happened to you but I run the script with either PyCharm or VS Code and it runs perfectly.

The only prerequisite (probably you skipped this step) is that you should first generate the credentials.json file, download it and put it into the same directory where you have the python script. In addition, I created an empty tokens.json file.

First, it will automatically open your default browser to indicate which Gmail account you want to specify. Next, it will output the labels of your Gmail account.

like image 149
Carlos F. Enguix Avatar answered Sep 05 '25 05:09

Carlos F. Enguix


My mistake was that I right clicked on my JSON file, selected rename, then named it credentials.JSON, which made the real file name credentials.JSON.JSON.

like image 28
Maxime Daigle Avatar answered Sep 05 '25 05:09

Maxime Daigle