I have an access to the not-mine private spreadsheet on the Google Sheet. When I access to that spreadsheet from browser, everything is fine. But, when I try to retrieve the content of that spreadsheet through Google Sheets API using Python, I am getting 403 error - "The caller does not have permission".
The problem is I can not ask the owner to give me one more permission for my API.
Can I retrieve the content of that spreadsheet somehow? Maybe using some tools?
You prolly can access it because you got the link from someone. But to access it using the API will require you to be added in the permissions. Worth checking would be this Google Drive SDK: Sharing Files tutorial video and the Permissions docs.
Related
Is it possible to access the google sheet of other account like that of my colleague based on my service account?
I am trying to automate google sheets with Python but most of my sheets are not in one account but on other accounts. I was wondering if I could interact with all of them based on one service account? I am using gspread library for my automation.
You can access the Google Spreadsheet(s) of your colleague if they have been shared with you or are all contained in a folder that has been shared with you. Depending on what you would like to do, most likely you need an editor permission.
In the same way, your service account can access the Google Spreadsheet(s) of your colleague if they have been shared with or are all contained in a folder that has been shared with your service account.
Actually I'm doing a attendance management system which gets the users Id, in time and uploads it to google sheet. Now the catch is inorder to write in google sheet we need internet, I know I can use Microsoft excel instead, But for certain reason I need to use Google sheets (because it includes a google script). Now I need to know whether I can write to google sheets without the internet or not!!
I understand that you want offline access to Sheets using Python. If I understood the situation correctly, you only need to give an eye to this guide about OAuth 2.0 for Desktop Apps, especially the part about calling Google APIs. Please, ask me any doubts about this approach.
Can I read a google spreadsheet which is open to people, but doesn't have a share option? There's a discussion here, but it's I need to have an authorization to click the share option.
Even copying by URL to my own Google spreadsheet may serve the purpose.
Update:
The idea was once I create a Google API, I should be able to create a .json file with a client email. In the share option, I'm supposed to provide the client email of .json file. You may see: Accessing Google Spreadsheet Data using Python.
This is the spreadsheet page where I'm not finding any Share option: https://docs.google.com/spreadsheets/d/e/2PACX-1vSc_2y5N0I67wDU38DjDh35IZSIS30rQf7_NYZhtYYGU1jJYT6_kDx4YpF-qw0LSlGsBYP8pqM_a1Pd/pubhtml#
Issue:
Publishing the contents of a spreadsheet to the web is not the same as making a spreadsheet public.
The URL you shared refers to spreadsheet contents that were published to the web following these steps. This published website is not the same as the original file where the data comes from, and so it doesn't have most of its functionalities, like a Share button (it doesn't make sense to have a Share button anyway, since this URL is already public).
Solution:
If you want to access the spreadsheet data using a Service Account, you would have to do one of the following (better to use method 1 if you have access to the spreadsheet):
Share the spreadsheet itself (not the published contents) with the Service Account, as explained in the link you referenced.
Use your application to fetch the website contents from the provided URL.
Reference:
Make Google Docs, Sheets, Slides & Forms public
I'm trying to make a function for creating Excel file and upload the excel file to Google Sheets (Google Drive maybe) via API (that's possible I think). Then, the function will publish Google Sheets to Web (like as Publish to Web function in Google Sheets web UI) in order to embed the Google Sheets in my website for Preview.
I could not figure out the API endpoint to do Publish To Web via Python.
There is the only Stackoverflow link I found out that someone did it via JS code (Google Sheets API: How to "publish to web" for embeddable sheet?), but it requires Authentication at the client side. For my case, I want to implement the function in Backend (server) with an Authorization Token (hard-code or managed by backend)
Answer:
Unfortunately there is not API endpoint for Google Drive nor Google Sheets which have the same functionality as the Publish to the web menu item in the Google Sheets UI.
More Information & Feature Request:
As it is possible to use the result of the Publish to the web menu item to embed a Sheet into a web page or Google Sites, it could be a useful API method for either Sheets or even Drive.
You can however let Google know that this is a feature that is important for the Sheets API and that you would like to request they implement it. Google's Issue Tracker[1] is a place for developers to report issues and make feature requests for their development services.
The page to file a Feature Request for the Google Sheets API can be found here[2].
References:
Google's Issue Tracker
File a Google Sheets API Feature Request
Instead of creating an excel sheet, try uploading directly to google sheets using gspread.
If you have an excel sheet you want to go through, try reading the cell in excel then writing that cell in google sheets
(Not my links, but should be a good reference to get started)
https://gspread.readthedocs.io/en/latest/
https://pypi.org/project/gspread/
https://www.youtube.com/watch?v=cnPlKLEGR7E
I have a google sheet which I set so that anyone can edit.
I noticed that in the gspread package, you can get a spreadsheet from the link alone. Example:
sht1 = gc.open_by_key('1wj9L7Hn779GKP0s4MblkU1wHcqaVcG_E2YKAo1vdof0')
Since anyone can edit, providing an auth code for access is not necessary. However, it seems that you always need to initialize it using an auth code.
Is there a way to edit a google sheet without any auth code?
This may get confused as a duplicate of this post Using gspread to read from a Google Drive Spreadsheet without logging in however, the solutions are to either have a client provide their auth code, or download the file directly, but no option to edit the file.
You want to edit Google Spreadsheet without logging in the Google for retrieving the authorization code.
You want to achieve this using gspread with python.
You have already been able to get and put values for Google Spreadsheet with Sheets API.
If my understanding is correct, how about this workaround? Please think of this as just one of several workarounds.
Workaround:
In this workaround, the service account is used. When the service account is used, you can get and put values for Google Spreadsheet without logging in Google account using the gspread.
In order to access to Google Spreadsheet with the service account, please do the following flow.
Create the service account and download the JSON file for using the service account.
Share the email address of the service account to the Google Spreadsheet you want to use.
You can see the email address at the downloaded JSON file.
Sample script:
The sample script for using the script of sht1 = gc.open_by_key('1wj9L7Hn779GKP0s4MblkU1wHcqaVcG_E2YKAo1vdof0') with the service account is as follows.
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://www.googleapis.com/auth/spreadsheets']
credentials = ServiceAccountCredentials.from_json_keyfile_name('### downloaded JSON file ###', scope)
gc = gspread.authorize(credentials)
sht1 = gc.open_by_key('1wj9L7Hn779GKP0s4MblkU1wHcqaVcG_E2YKAo1vdof0')
Please set ### downloaded JSON file ###.
References:
Using OAuth 2.0 for Server to Server Applications
Create a G Suite service account
Unfortunately you can't what you want without authentication. Is a mandatory thing.
In the end if you take a look at Gspread you could see that is actually using the Google Sheets API. And through the API you need to authenticate even when accessing (like in your case) public data.
There are other ways to authenticate with the API but sincerely I don't know if they are supported by Gspread (I believe that they are not).
So you could use some workaround proposed in the question that you mentioned.