Skip to content

Instantly share code, notes, and snippets.

@rcknr
Created December 24, 2012 13:59
Show Gist options
  • Save rcknr/4369338 to your computer and use it in GitHub Desktop.
Save rcknr/4369338 to your computer and use it in GitHub Desktop.
An example of using Drive API v2 in Apps Script. This will add "commenter" permission to a specified file. Note that not all types of documents support this permission type. To use this you need to create a project in API Console, enable Drive API in Services and get the API key.
function fileAddCommenter() {
authorize();
var fileId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // <-- id of the document
var key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // <-- developer key
var requestBody = {
role: "reader",
type: "user",
value: "someone@somewhere.com",
additionalRoles: ["commenter"]
};
var notify = false; // <-- e-mail notification parameter
var params = {method: "post",
oAuthServiceName: "drive",
oAuthUseToken: "always",
contentType: "application/json",
payload: Utilities.jsonStringify(requestBody)
};
var request = UrlFetchApp.fetch("https://www.googleapis.com/drive/v2/files/"+fileId+"/permissions?sendNotificationEmails="+String(notify)+"&key="+key, params);
Logger.log(request.getContentText());
}
function authorize() {
var oauthConfig = UrlFetchApp.addOAuthService("drive");
var scope = "https://www.googleapis.com/auth/drive";
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oauthConfig.setAuthorizationUrl("https://accounts.google.com/OAuthAuthorizeToken");
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment