Accessing google spreadsheet using shared link - python

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

Related

FileNotFoundError when using gspread

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'))

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 connect to Google Spreadsheets API?

I want to connect to the Google Spreadsheets API with Python.
So I do:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope=['https://www.googleapis.com/auth/spreadsheets',
'https://wwww.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('cred.json', scope)
gc = gspread.authorize(credentials)
wks = gc.open('datatest').Sheet1
print(wks.get_all_records)
But I get this error
HttpAccessTokenRefreshError: invalid_scope: https://www.googleapis.com/auth/spreadsheets is not a valid audience string.
How can I use Google-auth instead of gspread with this data?
you input 4w !!!!
X 'https://wwww.googleapis.com/auth/drive'
O 'https://www.googleapis.com/auth/drive'
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('CredentialsFile.json', scope)
ss_app = gspread.authorize(credentials=credentials) # make sure your Google Sheets API is enabled
sht = ss_app.open('sheet_name_xxxx') # In your spreadsheet, click the Share button and paste the client email as same as CredentialsFile.json.client_email, and make sure your Google Drive API is enabled
wks = sht.worksheet('worksheet_name_xxxx')
print(wks.acell('A1').value)
hope that it helps.

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)

Unable to authenticate Google API with Oauth2

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.

Categories