Determine if current user has write access for google sheet - javascript

I am using Google Sheets API from a Javascript application.
Users log in to my app and grant it permission read/write permission to google sheets.
I have hardcoded a document ID for a google sheet into my application.
I want to be able to make a rest query to determine the current users access rights to that particular document. The user may have:
no access
read only access
read write access
(I manually share the relevant access to the document to each user)
I am looking for a way of doing this. I have searched the sheets API and the drive API but I can't find any call.
At the moment I plan to create a function which will:
try and read the document - if I get an error return "NO ACCESS"
try and write the document - if I get an error return "READ ONLY"
return "READ WRITE"
It seems like a bad idea to determine write access by actually making a write to the document.
Does anyone have any better ideas?
I have tried listing the permissions using this API:
https://developers.google.com/drive/v3/reference/permissions/list
but I just get a list of permissions and this has to be parsed. Also my test read only user got forbidden.

Related

How to track user clicks on Phaser web-game?

So I've made a web game with the Phaser.js framework. Without a database, I want to track events/actions in the game. The data consist of at least two strings: time_stamp & event (i.e. "click the 'Play' button").
My current attempt is to write the data to my google sheets. How to do that?
I want something like:
Function for 'Play' button
playGame() {
// Go to gameplay scene
scene.start(Gameplay)
// Script to insert data to google sheet
submitEvent(timestamp, "click the 'Play' button"){
}
}
Or maybe there's an easier way to do the tracking? Need to be a free service and capable to store data online with javascript.
I tried the Google Sheet API but was confused with the doc, it seems like I need the player to log in and authorize, is it possible to avoid that? Most tutorials use HTML Form to send data, which I didn't use.
I believe your goal is as follows.
You want to put the values of timestamp and "click the 'Play' button" to your Google Spreadsheet using Javascript without authorization process.
In the current stage, in order to put a value to Spreadsheet using Sheets API, it is required to use the access token retrieved from OAuth2 and the service account. I'm worried that in this case, the script might be a bit complicated, and it might not your expected direction.
If you want to put a value to your Spreadsheet with a simple script without the authorization process, how about using Web Apps created by Google Apps Script? When Web Apps is used, your goal might be able to be achieved. When the method for putting a value to Spread sheet with Web Apps is implemented in Javascript, please do the following flow.
Usage:
1. Create Spreadsheet.
Please create Spreadsheet. You can create a new Spreadsheet by accessing https://docs.google.com/spreadsheets/create. In this case, the Spreadsheet is created in the root folder of your Google Drive. And please open the script editor on Spreadsheet.
2. Prepare Google Apps Script.
Please copy and paste the following script to the script editor and save the script.
function doGet(e) {
const { value1, value2 } = e.parameter;
SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].appendRow([value1, value2]);
return ContentService.createTextOutput("Done.");
}
3. Deploy Web Apps.
The detailed information can be seen in the official document.
Please set this using the new IDE of the script editor.
On the script editor, at the top right of the script editor, please click "click Deploy" -> "New deployment".
Please click "Select type" -> "Web App".
Please input the information about the Web App in the fields under "Deployment configuration".
Please select "Me" for "Execute as".
Please select "Anyone" for "Who has access".
Please click "Deploy" button.
When "The Web App requires you to authorize access to your data." is shown, please click "Authorize access" button. And, please authorize the scopes.
This authorization process is only one time. When you access Web Apps using Javascript, no authorization is required, because it has already been done here. I thought that this might be an important point to your expected direction.
Copy the URL of the Web App. It's like https://script.google.com/macros/s/###/exec. This URL is used for your HTML.
4. Testing.
When the values of value1 and value2 are timestamp and "click the 'Play' button", the sample script of Javascript is as follows. And, please set your Web Apps URL. And, please use this script in your script.
function sample(value1, value2) {
const webAppsURL = "https://script.google.com/macros/s/###/exec"; // Please set your Web Apps URL.
fetch(`${webAppsURL}?value1=${value1}&value2=${value2}`).then(async (res) => console.log(await res.text()));
}
When you run sample(value1, value2) by giving the values, the values are put to the 1st sheet of the Spreadsheet.
Note:
When you modified the Google Apps Script of Web Apps, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful about this.
You can see the detail of this in my report "Redeploying Web Apps without Changing URL of Web Apps for new IDE (Author: me)".
References:
Web Apps
Taking advantage of Web Apps with Google Apps Script (Author: me)

Get Google Ads customer id via login

I use “google-ads-api” to manage google ads. And api needs “customer_account_id” to initialization. However, I Couldn’t found how to get “customer_account_id” programmatically from user.
I used “react-google-login” in front-end, but there is no “customer_account_id” in the response. What is the best way to get customer_account_id?
You'll have to use the CustomerService's ListAccessibleCustomers method. This request does not require a customer_account_id and you can use it to obtain all Google Ads account IDs that the authenticated user has access to.
See the documentation for details.
I believe that in google-ads-api the relevant class is called AccessibleCustomersService.

Firebase limit user write access

i setup rules to limit user access like this:
.write": "auth != null && !root.child('blockedUsers').hasChild(auth.uid)",
The problem is, i dont know how to use it on the client side. I am assuming this is duplicate question, but i cant find anything on actual usage of the limited user access.
When user tries to create comment for example, i get an error that permission is denied. Thats desired result, problem is how do i check for the user write permission on the client ?
I was hoping for something like user.canWrite or something along those lines. All i am doing right now is just check if user was authenticated, which he was and there is no mention of read/write access rules in the user object as far as i can tell.
if (this.props.user) {
firebase.database().ref(`comments/${key}/rating`)
.transaction(
(value) => (value += rateValue)
)}
Thanks for any help.
By writing you rule you are setting an authorization to authenticated users to write to the specific node of your database.
Therefore, in the client, you only need to insure that your user is authenticated. This should be done through Firebase Authentication. See the doc for the web here: https://firebase.google.com/docs/auth/web/start
In case the user cannot write to this specific node (either because he/she is not authenticated or because his/her uid is listed under the blockedUsers) he/she will receive an error in the front-end.
Update following our comments below:
If you want to be able "to modify the client UI based on the user's role or access level" you should use another mechanism for setting up the authorization: Custom Claims. See the doc here: https://firebase.google.com/docs/auth/admin/custom-claims.
In this case, in the security rule, you would not check if there is a record under the 'blockedUsers' node but you would check the Claims attached to the token, as explained in the documentation.

How to update Google Cloud Storage ACL using Cloud Storage API in Javascript

I was studying the sample: https://developers.google.com/storage/docs/json_api/v1/json-api-javascript-samples
It works well and one of its demonstrations is getBucket/ObjectAccessControls. But update ACL is not in the sample.
The closest document I could find was https://developers.google.com/storage/docs/json_api/v1/bucketAccessControls/update?hl=zh-TW
But where should I put the "Request body" part? or use the generic gapi.client.request? So do I need to call gapi.client.storage.bucket/objectAccessControls.insert even if the generic gapi.client.request has been used?
Moreover, in the above document there is no description about where to put the ACL content, something like the xml scripts in https://developers.google.com/storage/docs/accesscontrol?hl=zh-tw
And also the cross origin settings: https://developers.google.com/storage/docs/cross-origin?hl=zh-tw

Share a Drive document without notifying user with Google Apps Script

I am creating a workflow process in Apps Script where a Doc is generated from a template and shared with various users for approval. The Script sends a customised email notifying a user that the document requires their approval but they also receive a second email at each stage in the process from the user whose Drive the document is stored in saying "User has shared a document with you". Is there any way of disabling these alerts? When you share a document manually from your Drive, there is a checkbox option that allows you to choose whether or not the user receives a notification. However, I cannot find a way to disable this notification with Apps Script.
I am using doc.addEditors(users) to share the document.
Many Thanks
Another option would be to use the Drive advanced service (which you should enable in the Resources menu in the Script Editor).
The code used should be
Drive.Permissions.insert(
{
'role': 'writer',
'type': 'user',
'value': 'bob#example.com'
},
fileId,
{
'sendNotificationEmails': 'false'
});
There is a simple solution if you are working with Google Docs or Google SpreadSheets. You can use DocumentApp or SpreadSheetApp to share your Docs or SpreadSheets without email notification:
DocumentApp
var doc = DocumentApp.openById('124144')
doc.addEditor('example#mail.com').addViewer('example2#mail.com')
SpreadSheetApp
var spreadSheet = SpreadsheetApp.openById('124144')
spreadSheet.addEditor('example#mail.com').addViewer('example2#mail.com')
However, if you are working with documents that aren't Docs or SpreadSheets, you must share then using DriveApp and email notification will be send.
This isn't possible at the moment. More information about this topic can be found here: https://code.google.com/p/google-apps-script-issues/issues/detail?id=2829
A workaround suggested in the comments of the above issue is to use DocsList instead:
DocsList, SpreadsheetApp and DocumentApp have addEditor and addViewer methods that do not result in notification emails.

Categories

Resources