Get access of google sheets of other account - python

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.

Related

How to Access google sheets offline with python

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.

Python reading a public google spreadsheet with no share option

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

In python, is it possible to edit a publicy shared google sheet, without an auth code?

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.

upload files to google drive without logging in

So with the google sheets api, I can use a credentials json file to edit a google sheet. I don't have to login again and again. Is it possible to do this same thing with the google drive api? I am making a Python webapp, and it is very inefficient to need to log into a google account every time I want to upload a file. I plan on only uploading files to one drive, and not multiple accounts. I have looked on stack overflow a couple times, and can only find documentation on how to login with a user account and this requires I login every time.
Best wishes,
Jake
You can do the same thing as in Sheets, follow the official Python Quickstart from the docs for instructions on how to do it.

Retrieve the content of access-permitted private google sheets from API?

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.

Categories