googleworkspace / python-samples

🐍 Python samples for Google Workspace APIs
Apache License 2.0
1.2k stars 894 forks source link

quickstart.py fails on error: No such file or directory: 'credentials.json' #134

Open ghost opened 4 years ago

ghost commented 4 years ago

Expected Behavior

Browser window should open requesting authorisation

Actual Behavior

No browser opens. Error logged to console: $ python quickstart.py Traceback (most recent call last): File "quickstart.py", line 52, in main() File "quickstart.py", line 29, in main 'credentials.json', SCOPES) File "/home/iarla/civetweb-instance-cal/cal/local/lib/python2.7/site-packages/google_auth_oauthlib/flow.py", line 196, in from_client_secrets_file with open(client_secrets_file, 'r') as json_file: IOError: [Errno 2] No such file or directory: 'credentials.json'

Steps to Reproduce the Problem

  1. run python quickstart.py as instructed in documentation https://developers.google.com/calendar/quickstart/python
  2. Observer no browser appears
  3. Observer error in terminal

Specifications

sqrrrl commented 4 years ago

Did you complete step 1 correctly and save the file as instructed?

darrenlimweiyang commented 4 years ago

@tenleftfingers It seems like you did not add the credentials.json file into working directory as instructed?

ghost commented 4 years ago

I was sure I put it in the right place, but I'll try again and come back (can't get to it this week). Thank you.

Seolta ó mo iPhone

On 11 Jan 2020, at 08:06, Darren Lim notifications@github.com wrote:

 @tenleftfingers It seems like you did not add the credentials.json file into working directory as instructed?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

Devarsh-leo commented 3 years ago

i am also facing the same issue when i developed a python script it worked and now i am opening it after few months it is showing: FileNotFoundError: [Errno 2] No such file or directory: 'credentials.json'

is there anyway to regenerate the credential ?

Devarsh-leo commented 3 years ago

[solved] I provided full path of credential and it worked. i guess my editor has parent folder of my project as its main folder.

shewitt-au commented 3 years ago

I am also having this issue. The first comment in this ticket says the following: "Did you complete step 1 correctly and save the file as instructed?"

Step one is "To install the Google client library for Python". I see no instructions to save a file. Am I missing something here?

rhadad commented 3 years ago

I was having the same problem. It looks like it's in the Prerequisites step now. On the Python Quickstart instructions page, https://developers.google.com/sheets/api/quickstart/python?authuser=3:

Underneath the note box that reads, "Note: For this quickstart, you are enabling the 'Google Sheets API'." is a bullet point that reads, "Authorization credentials for a desktop application. To create credentials for a desktop application, refer to Create credentials." With a link on the last two words, "Create credentials." This will take you to another page with some instructions that will eventually create the JSON file. Be sure to rename the file once it's downloaded to credentials.json.

xutiann commented 3 years ago

I'm having the same problem and I already rename the json file to credentials.json but it still shows the error. Can anyone advise me what I should do?

rhadad commented 3 years ago

It sounds like the computer can't find it, so it's likely in the wrong folder. Make sure it's in the working directory or that you change the filepath to wherever it's saved on your computer.

xutiann commented 3 years ago

How do know where is my working directory?

antoineneidecker commented 3 years ago

See this https://developers.google.com/workspace/guides/create-credentials?authuser=3

github-canadianlad commented 3 years ago

Alright, I figured it out. You need to jump a few hoops to download the JSON file (Remember to rename it to credentials.json)

1) Go to this link https://console.cloud.google.com/ 2) Create credential from

I was having the same problem. It looks like it's in the Prerequisites step now. On the Python Quickstart instructions page, https://developers.google.com/sheets/api/quickstart/python?authuser=3:

Underneath the note box that reads, "Note: For this quickstart, you are enabling the 'Google Sheets API'." is a bullet point that reads, "Authorization credentials for a desktop application. To create credentials for a desktop application, refer to Create credentials." With a link on the last two words, "Create credentials." This will take you to another page with some instructions that will eventually create the JSON file. Be sure to rename the file once it's downloaded to credentials.json.

Follows this guy's step

maxloosmu commented 2 years ago

I'm running pip and python from the Anaconda Prompt on my Windows 10 PC. I've 2 Google Drives running on virtual drives G and H. I've done Step 1 and 2 from https://developers.google.com/apps-script/api/quickstart/python.

When I got to Step 3, I got this error:

(base) C:\Users\Max\Downloads>python quickstart.py
Traceback (most recent call last):
  File "quickstart.py", line 81, in <module>
    main()
  File "quickstart.py", line 45, in main
    'credentials.json', SCOPES)
  File "C:\Users\Max\anaconda3\lib\site-packages\google_auth_oauthlib\flow.py", line 201, in from_client_secrets_file
    with open(client_secrets_file, "r") as json_file:
FileNotFoundError: [Errno 2] No such file or directory: 'credentials.json'

In the quickstart.py file, I've tried changing this:

flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)

To this:

flow = InstalledAppFlow.from_client_secrets_file('C:\Users\Max\Downloads\credentials.json', SCOPES)

And to this:

flow = InstalledAppFlow.from_client_secrets_file('G:\My Drive\Google Sheets\credentials.json', SCOPES)

I've a copy of credentials.json at both locations, but both didn't work. I've tried setting the Windows Path variable to include G:\My Drive\Google Sheets, but that didn't work either. I also did this from the command prompt based on these instructions: https://cloud.google.com/docs/authentication/getting-started#windows.

set GOOGLE_APPLICATION_CREDENTIALS="G:\My Drive\Google Sheets\credentials.json"

But that didn't work too.

I then tried the same steps with new credentials.json and quickstart.py based on instructions from: https://developers.google.com/sheets/api/quickstart/python.

I got a similar error:

(base) G:\My Drive\Google Sheets>python quickstart.py
Traceback (most recent call last):
  File "quickstart.py", line 54, in <module>
    main()
  File "quickstart.py", line 31, in main
    'credentials.json', SCOPES)
  File "C:\Users\Max\anaconda3\lib\site-packages\google_auth_oauthlib\flow.py", line 201, in from_client_secrets_file
    with open(client_secrets_file, "r") as json_file:
FileNotFoundError: [Errno 2] No such file or directory: 'credentials.json'

Does anyone know how to resolve this? Is there a way to set the working directory?

maxloosmu commented 2 years ago

My problem has been resolved. It arised from a typo in the filename of the json file. I had mistakenly added an extra json to the filename, thereby causing the error: credentials.json.json.

GaoxieNju commented 2 years ago

image download the file and rename

biduradhikari commented 2 years ago

Rather than hard coding the path names (which change if you copy the file elsewhere), you should try getting the current directory path. Changing the code from line 31 as follows should solve the issue.

CURR_DIR = os.path.dirname(os.path.realpath(__file__)) print(CURR_DIR) credential_file=str(CURR_DIR)+'/credentials.json' flow = InstalledAppFlow.from_client_secrets_file( credential_file, SCOPES)

Aran77 commented 1 year ago

if you rename the file to Credentials.json in windows you will find the name will be credentials.json.json in the cli window. rename the file to credentials.json in cli.

MartianTribe commented 1 year ago

I resolved this by simply pointing to the full path for the file:

credsPath = os.path.expanduser("~/Desktop/Kargo/Development/Python/KargoPy/modules/credentials.json")