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

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.



Solution 1:[1]

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.

Solution 2:[2]

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.

Solution 3:[3]

If it is in the same location as the script, you can try:

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

Solution 4:[4]

I had the same issue and it turned out that the problem was with the IDE I'm using (Mu 1.1.0). Once I downloaded the JSON file, renamed it, placed it in the directory to where the error was pointing to, and ran everything with cmd, it worked as it should.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Felipe Augusto
Solution 2 charlesreid1
Solution 3 Barathi K
Solution 4 BrokenBenchmark