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)
Related
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'))
I am new to google apis. I am looking to read a google spreadsheet via a python program and following instructions found here:
https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html
I have a google sheet called Legislators 2017 that I am using for this.
The following is the code to print out some content from the sheet.
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('google_drive_oct_6_2020.json', scope)
client = gspread.authorize(creds)
sheet = client.open("Legislators 2017").sheet1
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)
The above works.
I am inclined to use the smallest scope possible in general, and would like to use
https://www.googleapis.com/auth/spreadsheets or
https://www.googleapis.com/auth/spreadsheets.readonly
but they don't work and I get an exception with the following message:
Insufficient Permission: Request had insufficient authentication scopes.
What am I missing ?
When client.open("Legislators 2017") is used, the method of "Files: list" in Drive API is used. Ref By this, the scope of Drive API is required to be used. In your script, https://www.googleapis.com/auth/drive or https://www.googleapis.com/auth/drive.readonly are required.
When you don't want to use the scopes of Drive API and want to use the smallest scope possible in general, how about the following modification?
From:
scope = ['https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('google_drive_oct_6_2020.json', scope)
client = gspread.authorize(creds)
sheet = client.open("Legislators 2017").sheet1
To:
scope = ['https://www.googleapis.com/auth/spreadsheets.readonly']
creds = ServiceAccountCredentials.from_json_keyfile_name('google_drive_oct_6_2020.json', scope)
client = gspread.authorize(creds)
sheet = client.open_by_key("###").sheet1
### is the Spreadsheet ID of Legislators 2017.
In this modification, the scope of https://www.googleapis.com/auth/spreadsheets.readonly can be used. Also, https://www.googleapis.com/auth/spreadsheets can be used.
References:
open_by_key(key)
Method: spreadsheets.values.get
It seems that get_all_records() uses the method of "spreadsheets.values.get". Ref
When creating the Authentication credentials from GCP, you need to state the level of access given to a Authentication Key.
You can then access that level of access, or less.
According to the guide you are referring:
Name the service account and grant it a Project Role of Editor.
Let it be stated that this is the first time I'm working with Google Cloud, so this is probably a noob question.
I want to use the Google Cloud Speech library to do some speech to text on an audio file. I also want to explicitly state within the code which Google Cloud service account credentials to use by giving the private key file. How do I do so?
It seems that what I want is a mix of this quickstart for speech recognition and this example of how to set credentials within the code (the explicit() part).
I tried doing so, but explicit() uses google.cloud.storage to set the client,
from google.cloud import storage
storage_client = storage.Client.from_service_account_json('service_account.json')
in order to make the API request.
Setting
client = storage.Client.from_service_account_json('service_account.json')
and then running
client.recognize(config, audio)
obviously throws an error saying that client doesn't have that attribute. My guess is what I need is something similar, but for google.cloud.speech? I have tried looking through the documentation - am I missing something?
Almost all of the Google Client libraries follow the same design pattern for credentials. In the example below, the code loads the credentials and then creates the client using those credentials.
Link to the Speech Client
from google.oauth2 import service_account
from google.cloud import speech_v1
from google.cloud.speech_v1 import enums
SCOPES = ["https://www.googleapis.com/auth/cloud-platform"]
SERVICE_ACCOUNT_FILE = 'service-account.json'
cred = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
client = speech_v1.SpeechClient(credentials=cred)
OR (Notice not specifying scopes which is usually OK)
client = speech_v1.SpeechClient.from_service_account_file(SERVICE_ACCOUNT_FILE)
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
I had a Python script that I had authorized to access a Google Spreadsheet. But when now when I run it, it throws the exception:
oauth2client.client.AccessTokenRefreshError: invalid_client: The OAuth client was not found.
My auth code looks like:
import json
import gspread
from oauth2client.client import SignedJwtAssertionCredentials
json_key = json.load(open('myapp-credentials.json'))
scope = ['https://spreadsheets.google.com/feeds']
credentials = SignedJwtAssertionCredentials(
json_key['client_email'],
json_key['private_key'], scope)
gc = gspread.authorize(credentials)
I've not changed anything in the account, so I don't know why it would suddenly stop working. If I login to https://console.developers.google.com/apis/credentials, I see the app is still registered. How do I diagnose this and re-enable its access? Googling this error unfortunately finds dozens of potential causes.
The class SignedJwtAssertionCredentials has been removed from the source code as of February 5, 2016, and its functionality replaced with that of ServiceAccountCredentials. This may be causing your error.
It appears that you'll want to use something like the following to generate your credentials:
credentials = ServiceAccountCredentials.from_json_keyfile_name('myapp-credentials.json', scope)
This Github issue thread might be helpful, as well.