I'm currently working on a private script that allows for data warehouse ETL work to run. I'm saving my env secrets on a cloud vault. The problem comes when a script runs and pulls information from a private google sheet, this requires a token generated from a project on G Cloud, however, it appears the token has a limited lifespan, is there a way to get a perma-token that does not expire? (Without publishing the project)
Thanks
The issue is that you haven't published the project, it will expire every 7 days.
To solve the issue you will need to publish the project (you can leave it as private) by following the steps:
1. Access to console.cloud.google.com
2. Make sure that you have selected the correct project.
3. Click the 3 lines for the menu.
4. Search for "Marketplace."
5. At the top, in the search bar, type "Google Workspace Marketplace SDK," and press enter.
6. On the Google Workspace Marketplace SDK page, click Enable.
7. Create the store listing
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.
I have full edit access to a Google Sheet not owned by me. I want to be able to write to the spreadsheet using Python without Google API authorization. I checked several available packages (gdata, gspread etc.) and seems all of them ask for the credentials.
I was also able to read the content of a spreadsheet without authorization using requests or pd.read_csv() by pandas (I tweaked the URL by changing the last part saying ...edit#gid=... to ...export?format=csv&gid=...). Yet, when sending a POST request to the same URL I received 200 status code but the same old empty spreadsheet.
Any help is highly appreciated.
I believe your goal as follows.
You want to put the values to the publicly shared Google Spreadsheet using python.
In this case, you want to access to the Spreadsheet without authorization.
For this, how about this answer?
Issue and workaround:
In order to put the values to the publicly shared Google Spreadsheet, the POST method is used. In this case, it is required to use the access token. On the other hand, in the case of the GET method, when the Sheets API is used, an API key can be used. And the endpoints like exportLinks, you can retrieve the values without the API key. These are the specification of Google side.
Under this condition, in order to achieve your goal, I would like to propose the following 2 patterns.
Pattern 1:
In this pattern, I would like to propose to access to the Spreadsheet using the access token retrieved from the service account. In this case, the script can be simpler.
Sample script:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
spreadsheetId = "###" # Please set the Spreadsheet ID.
scope = ['https://www.googleapis.com/auth/spreadsheets']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(spreadsheetId)
worksheet = spreadsheet.sheet1
worksheet.update_acell('A1', 'sample')
In this sample script, sample is put to the cell "A1" of the 1st tab in the publicly shared Spreadsheet using gspread.
Pattern 2:
In this pattern, I would like to propose to access to the Spreadsheet using the Web Apps created by Google Apps Script as the wrapper API. In this case, the python script is more simpler.
Usage:
Please do the following flow.
1. Create new project of Google Apps Script.
Sample script of Web Apps is a Google Apps Script. So please create a project of Google Apps Script.
If you want to directly create it, please access to https://script.new/. In this case, if you are not logged in Google, the log in screen is opened. So please log in to Google. By this, the script editor of Google Apps Script is opened.
2. Prepare script.
Please copy and paste the following script (Google Apps Script) to the script editor. And please enable Google Sheets API at Advanced Google services. This script is for the Web Apps.
function doPost(e) {
try {
const spreadsheetId = e.parameter.spreadsheetId;
const obj = JSON.parse(e.postData.contents);
const resource = obj.body;
const range = obj.arguments.range;
const valueInputOption = obj.arguments.valueInputOption;
Sheets.Spreadsheets.Values.update(resource, spreadsheetId, range, {valueInputOption: valueInputOption});
return ContentService.createTextOutput("ok");
} catch(e) {
return ContentService.createTextOutput(JSON.stringify(e));
}
}
In this case, the POST method is used.
In this sample script, as a test script, the values are put to the Spreadsheet using the method of spreadsheets.values.update in Sheets API.
3. Deploy Web Apps.
On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
Select "Me" for "Execute the app as:".
By this, the script is run as the owner.
Select "Anyone, even anonymous" for "Who has access to the app:".
In this case, no access token is required to be request. I think that I recommend this setting for your goal.
Of course, you can also use the access token. At that time, please set this to "Anyone".
Click "Deploy" button as new "Project version".
Automatically open a dialog box of "Authorization required".
Click "Review Permissions".
Select own account.
Click "Advanced" at "This app isn't verified".
Click "Go to ### project name ###(unsafe)"
Click "Allow" button.
Click "OK".
Copy the URL of Web Apps. It's like https://script.google.com/macros/s/###/exec.
When you modified the Google Apps Script, please redeploy as new version. By this, the modified script is reflected to Web Apps. Please be careful this.
4. Run the function using Web Apps.
This is a sample python script for requesting Web Apps. Please set your Web Apps URL, Spreadsheet ID and range.
import json
import requests
spreadsheet_id = '###' # Please set the Spreadsheet ID.
body = {
"arguments": {"range": "Sheet1!A1", "valueInputOption": "USER_ENTERED"},
"body": {"values": [["sample"]]}
}
url = 'https://script.google.com/macros/s/###/exec?spreadsheetId=' + spreadsheet_id
res = requests.post(url, json.dumps(body), headers={'Content-Type': 'application/json'})
print(res.text)
In this sample script, sample is put to the cell "A1" of the 1st tab in the publicly shared Spreadsheet.
In this case, no authorization is required in the python script, because it has already been done when Web Apps is deployed.
Note:
When you modified the script of Web Apps, please redeploy the Web Apps as new version. By this, the latest script is reflected to the Web Apps. Please be careful this.
References:
Web Apps
Taking advantage of Web Apps with Google Apps Script
Advanced Google services
publish a Google Spreadsheet through Google Apps Scripts
spreadsheets.values.update
I am trying to create a google cloud function that is called when a user clicks a button in google sheets. This function goes through the data and updates another sheet. I have been using pygsheets to do this but I would like to use google cloud functions so that anyone can update the sheet without emailing me to open a terminal and run the script.
My initial error message says that when i run pygsheets.authorize() that this method is trying to write in a read only file system. I have tried to make my own custom credentials but have gotten nowhere with them. I have linked the conversation I had with the maintainer of the pygsheet project.
I am self taught and thus would happily take any advice on how to resolve this issue. I would like to keep using pygsheets if possible as I have not seen any python google sheets tools on par with it. Please help this issue has stumped me for a couple of weeks.
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.