FileNotFoundError when using gspread - python

I am trying to make a program that can add user inputs into a google sheet so I can document it. I have had smooth sailing up until now. I am following This tutorial. Now when I run my code it outputs the error:
FileNotFoundError: [Errno 2] No such file or directory: 'creds.json'
Here is the code I am running
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/sprea...',"https://www.googleapis.com/auth/drive...","https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
client = gspread.authorize(creds)
The file it titled "creds.json" and I currently have it sitting on my desktop (Could this be the issue?)
Thank-you for your help!

If you use gspread and authenticate with a service account you can simply follow the documentation
The function service_account takes the file path of the service account JSON file and returns the client ready to use.
Like so:
gc = gspread.service_account("./my_ceeds.json")
sh = gc.open("Example spreadsheet")
print(sh.sheet1.get('A1'))

Related

Error 403 google spreadsheets gspread when open in console

I'm trying to repeat the guide (https://www.youtube.com/watch?v=vISRn5qFrkM) - generated client_secrets.json, put it in the DAGs folder (because then I will run the script from airflow)
But despite the client secret-still gives the error 403-not enough rights , how can I fix it?
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pprint
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json',scope)
client = gspread.authorize(creds)
sheet = client.open('Data base').sheet4
result = sheet.get_all_records()
print(result)
self solved - apparently when you use the file name it refers to the disk API to find this file
therefore, you must also log in to drive. to do this, you need to add it to the scope
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pprint
scope = ["https://spreadsheets.google.com/feeds","https://www.googleapis.com/auth/spreadsheets.readonly", "https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json',scope)
client = gspread.authorize(creds)
sheet = client.open('Data base').worksheet("Sum Data")
result = sheet.get_all_records()
print(result)

How to properly setup credentials manually for Cloud Speech-to-Text API in python?

Ok, this is my first question in the site so I going to try to be clear.
I am trying to build a speech recognition application in the raspberry pi with python and the Cloud Speech-to-Text API. While trying to set the credentials for the application defining a variable in the Terminal (following the steps shown here: https://cloud.google.com/speech-to-text/docs/reference/libraries#client-libraries-usage-python) I get the following error:
Traceback (most recent call last):
File "/home/pi/Documents/pythonPrograms/GoogleSpeech.py", line 15, in <module>
client = speech.SpeechClient()
File "/usr/local/lib/python3.5/dist-packages/google/cloud/speech_v1/gapic/speech_client.py", line 137, in __init__
credentials=credentials,
File "/usr/local/lib/python3.5/dist-packages/google/cloud/speech_v1/gapic/transports/speech_grpc_transport.py", line 63, in __init__
credentials=credentials,
File "/usr/local/lib/python3.5/dist-packages/google/cloud/speech_v1/gapic/transports/speech_grpc_transport.py", line 98, in create_channel
scopes=cls._OAUTH_SCOPES,
File "/usr/local/lib/python3.5/dist-packages/google/api_core/grpc_helpers.py", line 177, in create_channel
credentials, _ = google.auth.default(scopes=scopes)
File "/usr/local/lib/python3.5/dist-packages/google/auth/_default.py", line 306, in default
raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started
As that didn't work, I tried to set the credentials manually inside the code. The problem is, I keep getting the same error (probably because I'm not doing it right). Here is my code right now:
import io
import os
# Imports the Google Cloud client library
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file("/root/Downloads/key.json")
scoped_credentials = credentials.with_scopes(["https://www.googleapis.com/auth/cloud-platform"])
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
# Instantiates a client
client = speech.SpeechClient()
# The name of the audio file to transcribe
file_name = os.path.join(
os.path.dirname(__file__),
'resources',
'audio.raw')
# Loads the audio into memory
with io.open(file_name, 'rb') as audio_file:
content = audio_file.read()
audio = types.RecognitionAudio(content=content)
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code='en-US')
# Detects speech in the audio file
response = client.recognize(config, audio)
for result in response.results:
print('Transcript: {}'.format(result.alternatives[0].transcript))
While searching for a solution I tried eliminating the part of the code that says:
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
To what I get:
Traceback (most recent call last):
File "/home/pi/Documents/pythonPrograms/GoogleSpeech.py", line 12, in <module>
client = speech.SpeechClient()
NameError: name 'speech' is not defined
Thus, I suppose the problem is within the way that I imported that, and not in the credential itself. It is important to add that I activated the API in my Google Cloud account project.
Any help would be really appreciated.
This post is over 2 years old but I bumped into the same problem (I wanted to explicitly set the credentials in the code for testing)
The only thing you need to do is to pass the credential as credential argument to SpeechClient as follows:
import io
import os
# Imports the Google Cloud client library
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file("/path/to/key.json")
scoped_credentials = credentials.with_scopes(["https://www.googleapis.com/auth/cloud-platform"])
from google.cloud import speech
# Instantiates a client
client = speech.SpeechClient(credentials=credentials)
Alternatively, you could also use from_service_account_file method directly in SpeechClient:
from google.cloud import speech
# Instantiates a client
client = speech.SpeechClient.from_service_account_file("/path/to/key.json")
If your credentials set-up is OK any of the options should work without a problem.
The error message clearly indicates that environment variable GOOGLE_APPLICATION_CREDENTIALS is not set.
The ReadMe file for the reference code mentions that you have to setup the authentication. Basically you need to create a service account, give the service account necessary permissions(= set a role, this depends on what do you want to do with the account), download the credentials json file and set GOOGLE_APPLICATION_CREDENTIALS environment variable to point to your json file. Actual steps vary depending on your OS, whole process is documented here.
I use os command to solve Google-Credential issue. set GOOGLE_APPLICATION_CREDENTIALS environment variable in Python code following.
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path of your JSON credential file"
This will be worth for the issue.

Google Sheets - Python Access timeout

I'am trying to read and write from and to a Google Spreadsheet. I've found this tutorial online, which seems to work for a lot of people:
https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html?utm_source=youtube&utm_medium=video&utm_campaign=youtube_python_google_sheets
What I did so far:
Created the credentials according to the guide and downloaded the .json file
Shared a copy of the Legislator Spreadsheet (now in my google Drive) with the client e-mail.
I received an E-Mail after sharing that the message wasn't delivered as the service account domain wasn't found.
I adopted the code
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)
sheet = client.open("Copy of Legislators 2017", scope)
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)
The client_secret.json file is in the same folder as the project.
But when I run the code, I get the following error:
The Japanese says: ”It was not possible to connect because the connected callee did not respond correctly even after a certain period of time. Or the connected host failed because the connected host did not respond.”
There seems to be a problem with the log in, but I have no clue how to fix it.
Also, while researching, I found that the scope part is always different. Can someone explain to me what needs to be insert there?
Does anyone have an experience with it? Thank you for your help :)
Make sure you're OAuthclientID is valid and you've enabled Sheets API in your Google Dev Console.
Then, try to use the Sheetsv4 scope instead. I remember encountering some errors when I used the scope for v3. So hopefully, this helps you:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pprint
scope = ['https://www.googleapis.com/auth/spreadsheets']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)
sheet = client.open('NAME_OF_GOOGLE_SHEET').sheet1
pp = pprint.PrettyPrinter()
# get all the records
result = sheet.get_all_records()
pp.pprint(result)

Accessing google spreadsheet using shared link

I am building a python app where user can provide 'Anyone can edit' link to their spreadsheet and data can be read from there. I am using python's gspread module but it throws SpreadSheetNotFound error.
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('myauth.json', scope)
gc = gspread.authorize(credentials)
wks = gc.open_by_url(urls_given_by_user_having_edit_permission)
Above code is not working. Is there any way to achieve this?
wks.share('', perm_type='anyone', role='reader')
Read permission for all people with link. reference https://gspread.readthedocs.io/en/latest/api.html#gspread.models.Spreadsheet.share

Google API quickstart.py error KeyError: '_module'

Using Gmail API. My client secret file is downloaded and working for Ruby. When I try the quickstart.py (python) version I get this error
File "quickstart.py", line 70, in <module>
main()
File "quickstart.py", line 55, in main
credentials = get_credentials()
File "quickstart.py", line 38, in get_credentials
credentials = store.get()
File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 374, in get
return self.locked_get()
File "/Library/Python/2.7/site-packages/oauth2client/file.py", line 79, in locked_get
credentials = Credentials.new_from_json(content)
File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 281, in new_from_json
module = data['_module']
KeyError: '_module'
I have not changed the file at all, just added the client_secret.json to that working directory and also install the google-api-python-client. My python code came from here: https://developers.google.com/gmail/api/quickstart/python
Try replacing creds = store.get() with creds = None temporarily. If this works, you can refactor your code to always start with flow-based credentials instantiation. This worked for me. It seems Google samples are out of sync with their oauth2client.
oauth2client is trying to load credentials from a json file with an incorrect structure.
Maybe the Ruby client uses a different file format but I'd be surprised. Are you sure you didn't save client_secret.json as ~/.credentials/gmail-quickstart.json accidentally?
Regardless, removing ~/.credentials/gmail-quickstart.json and re-authenticating will generate a new credentials file with the correct structure.
I am learning Python myself and had a similar problem, but with the Calendar API example. It turned out that it was a typo with regards to the SCOPE.
## Typo - Invalid definition
SCOPES = 'https://ww.googleapies.com/auth/calendar.readonly'
## Correct Value for SCOPE
SCOPES = 'https://www.googleapis.com/auth/calendar'
Also, Matt's answer help point me in the right direction. The gmail-quickstart.json is not the same thing as the client_secret.json. The client_secret.json allows you to make a request for an OAuth2 token. While the gmail-quickstart.json contains the issued token and meta-data associated with it. The gmail-quickstart.json isn't created until you successfully login.
One last thought, in order to log in successfully, the quickstart.py app launched an instance of my web-browser (Firefox) and went to the Google login screen. In order for Firefox to run properly, I had to set my DISPLAY variable properly first.
$ export DISPLAY=:0
$ xhost +
access control disabled, clients can connect from any host
In this GitHub issue: error "KeyError: '_module'" when running gdrive_upload.py
sputnik-dev answared on 10 Jan 2016:
If someone have the same issue : auth_token.txt and client_secret.json are not the same! Don't link the client_secret.json from google API console. The file will be automatically created by the script.
Wrong way: gauth.SaveCredentialsFile("client_secret.json")
Right way: gauth.SaveCredentialsFile("<any random name>.json")
Extra:
PyDrive code that automate google drive api authetication. Use the browser just one time to authenticate and never more. It saves your credential data on mycreds.json :)
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LoadCredentialsFile("mycreds.json")
if gauth.credentials is None:
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
gauth.Refresh()
else:
gauth.Authorize()
gauth.SaveCredentialsFile("mycreds.json")
I solved this by moving the client_secret.json to the same directory as the py file that is trying to read it (quickstart.py), mine was on the Desktop while i had saved the json to Documents. I saved the json to the Desktop too and boy, It flew!.
I dont know why it doesnt work when they are in different directories, defining a custom credential_path doesn't help.
I followed several recommendations listed here:
moving the client_secret.json to the same directory as the py file that is trying to read it, as recommended by simic0de.
Working with skirill idea, instead of completely eliminating the 'credentials=store.get()', I decided to handle the exception, so it would work with flow-based credentials instantiation.
The final code is:
try:
credentials = store.get()
except:
print('Working with flow-based credentials instantiation')
You can write your own code in the except line. This approach will allow the store.get() command to work when conditions are met.

Categories