question on accessing or getting specific google app script query parameter - google-apps-script

I was wondering if I can access a specific query parameter from a method from activities.list API
specifically this piece of code
activity.events[0].parameters
I've already tried accessing through array like this:
activity.events[0].parameters[2]
but the output is not consistent, for instance index 2 is not what I need instead its a different query parameter. What I need is for example I need to access
organizer_email
parameter specifically from the API (https://developers.google.com/admin-sdk/reports/v1/appendix/activity/meet)
I'll attach my sample code here. its from google api examples
(https://developers.google.com/admin-sdk/reports/v1/quickstart/apps-script)
/**
* List login events for a G Suite domain.
* #see https://developers.google.com/admin-sdk/reports/reference/rest/v1/activities/list
*/
function getData() {
var today = new Date();
var oneWeekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);
var timezone = Session.getScriptTimeZone();
var date = Utilities.formatDate(oneWeekAgo, timezone, 'yyyy-MM-dd');
const userKey = 'all';
const applicationName = 'meet';
var pageToken;
const optionalArgs = {
maxResults: 10,
pageToken: pageToken,
};
try {
const response = AdminReports.Activities.list(userKey, applicationName, optionalArgs);
const activities = response.items;
if (!activities || activities.length === 0) {
Logger.log('No logins found.');
return;
}
// Print login events
Logger.log('MEET LOGS:');
for (const activity of activities) {
Logger.log('(%s) %s %s', activity.events[0].parameters, activity.id.time, activity.id.time);
}
} catch (err) {
// TODO (developer)- Handle exception from the Report API
Logger.log('Failed with error %s', err.message);
}
}
sample array / json result (2 results)
[
{"name":"video_send_seconds","intValue":"40"},
{"name":"screencast_recv_bitrate_kbps_mean","intValue":"128"},
{"value":"email_address","name":"identifier_type"},
{"name":"audio_send_bitrate_kbps_mean","intValue":"2"},
{"name":"video_send_packet_loss_max","intValue":"1"},
{"value":"meetings_android_214151224321356190","name":"endpoint_id"},
{"name":"device_type","value":"android"},{"intValue":"0","name":"video_send_packet_loss_mean"},
{"intValue":"240","name":"video_recv_long_side_median_pixels"},
{"name":"screencast_recv_long_side_median_pixels","intValue":"1072"},
{"name":"screencast_send_seconds","intValue":"0"},
{"name":"video_send_fps_mean","intValue":"15"},
{"intValue":"4","name":"audio_send_packet_loss_max"},
{"intValue":"132","name":"video_recv_short_side_median_pixels"},
{"intValue":"0","name":"video_recv_packet_loss_mean"},
{"name":"screencast_recv_fps_mean","intValue":"8"},
{"intValue":"1267","name":"audio_recv_seconds"},
{"intValue":"0","name":"network_congestion"},
{"name":"network_estimated_download_kbps_mean","intValue":"1398"},
{"intValue":"0","name":"audio_send_packet_loss_mean"},
{"name":"network_transport_protocol","value":"udp"},
{"intValue":"1269","name":"duration_seconds"},
{"intValue":"103","name":"video_send_bitrate_kbps_mean"},
{"name":"identifier","value":"xxxxxx.xxxxx#xxxx.xxx"},
{"intValue":"78","name":"audio_recv_packet_loss_max"},
{"intValue":"10","name":"video_recv_fps_mean"},
{"intValue":"3","name":"audio_recv_packet_loss_mean"},
{"name":"network_recv_jitter_msec_max","intValue":"205"},
{"name":"organizer_email","value":"xxxxxx#xxxxx.xxx"},
{"name":"screencast_recv_short_side_median_pixels","intValue":"480"},
{"name":"network_recv_jitter_msec_mean","intValue":"19"},
{"name":"audio_send_seconds","intValue":"1267"},
{"value":"xxxxxxxxxx","name":"display_name"},
{"name":"screencast_recv_packet_loss_max","intValue":"0"},
{"name":"video_recv_seconds","intValue":"71"},
{"intValue":"35","name":"network_rtt_msec_mean"},
{"name":"video_send_long_side_median_pixels","intValue":"240"},
{"name":"screencast_recv_packet_loss_mean","intValue":"0"},
{"value":"fjqip32o1ru139DFSA2wer23","name":"conference_id"},
{"intValue":"883","name":"screencast_recv_seconds"},
{"name":"product_type","value":"meet"},
{"name":"network_estimated_upload_kbps_mean","intValue":"87"},
{"name":"video_send_short_side_median_pixels","intValue":"132"},
{"intValue":"0","name":"video_recv_packet_loss_max"},
{"name":"meeting_code","value":"ZXWEREHXSG"},
{"name":"is_external","boolValue":true}
]
2nd result:
[
{"name":"video_send_seconds","intValue":"0"},
{"name":"identifier_type","value":"email_address"},
{"name":"audio_send_bitrate_kbps_mean","intValue":"5"},
{"value":"meetings_android_23adjfuhvioalu23xpiow;","name":"endpoint_id"},
{"name":"device_type","value":"android"},
{"intValue":"0","name":"screencast_send_seconds"},
{"intValue":"0","name":"audio_recv_seconds"},
{"intValue":"0","name":"network_congestion"},
{"name":"network_estimated_download_kbps_mean","intValue":"0"},
{"value":"udp","name":"network_transport_protocol"},
{"name":"duration_seconds","intValue":"9"},
{"value":"xxxxxxxxxx.xxxxxxxxx#xxxxxx.xxxxxx","name":"identifier"},
{"value":"xxxxxxxxxxx#xxxxxxxx.xxxxx","name":"organizer_email"},
{"name":"audio_send_seconds","intValue":"7"},
{"name":"display_name","value":"xxxxxxxxxxxxx"},
{"intValue":"0","name":"video_recv_seconds"},
{"intValue":"30","name":"network_rtt_msec_mean"},
{"name":"conference_id","value":"4ersdfgwe3re43edf3s"},
{"name":"screencast_recv_seconds","intValue":"0"},
{"name":"product_type","value":"meet"},
{"name":"network_estimated_upload_kbps_mean","intValue":"0"},
{"name":"meeting_code","value":"43eradsfdas23hgrf"},
{"boolValue":true,"name":"is_external"}
]

found the answer
example usage:
activity.events[0].parameters.find(x => x.name ==='duration_seconds').intValue

Related

list activities on google drive

I would like to be able to upload my file activities with API from google drive activity or other ideas. Each of my files are stored in a tree structure that contains the name of my client. And in the following put it in an excel file in order to be able to sort it correctly for example if a file has been modified more than 2 times in one day, it is validated
Currently, I used the code provided by google but I can't find the directory of these modified files
I thank you in advance, I really block, other ideas or API are welcome.
Sincerly
/**
* Lists 10 activity for a Drive user.
* #see https://developers.google.com/drive/activity/v2/reference/rest/v2/activity/query
*/
function listDriveActivity() {
const request = {
"ancestor_name": "items/root",
"filter": "time >= \"2022-06-01T00:00:00Z\" time < \"2022-06-30T00:00:00Z\" detail.action_detail_case:EDIT",
"consolidation_strategy": { "legacy": {} },
"page_size": 10,
};
try {
// Activity.query method is used Query past activity in Google Drive.
const response = DriveActivity.Activity.query(request);
const activities = response.activities;
if (!activities || activities.length === 0) {
console.log('No activity.');
return;
}
console.log('Recent activity:');
for (const activity of activities) {
// get time information of activity.
const time = getTimeInfo(activity);
// get the action details/information
const action = getActionInfo(activity.primaryActionDetail);
// get the actor's details of activity
const actors = activity.actors.map(getActorInfo);
// get target information of activity.
const targets = activity.targets.map(getTargetInfo);
// print the time,actor,action and targets of drive activity.
console.log('%s: %s, %s, %s', time, actors, action, targets);
}
} catch (err) {
// TODO (developer) - Handle error from drive activity API
console.log('Failed with an error %s', err.message);
}
}
/**
* #param {object} object
* #return {string} Returns the name of a set property in an object, or else "unknown".
*/
function getOneOf(object) {
for (const key in object) {
return key;
}
return 'unknown';
}
/**
* #param {object} activity Activity object.
* #return {string} Returns a time associated with an activity.
*/
function getTimeInfo(activity) {
if ('timestamp' in activity) {
return activity.timestamp;
}
if ('timeRange' in activity) {
return activity.timeRange.endTime;
}
return 'unknown';
}
/**
* #param {object} actionDetail The primary action details of the activity.
* #return {string} Returns the type of action.
*/
function getActionInfo(actionDetail) {
return getOneOf(actionDetail);
}
/**
* #param {object} user The User object.
* #return {string} Returns user information, or the type of user if not a known user.
*/
function getUserInfo(user) {
if ('knownUser' in user) {
const knownUser = user.knownUser;
const isMe = knownUser.isCurrentUser || false;
return isMe ? 'people/me' : knownUser.personName;
}
return getOneOf(user);
}
/**
* #param {object} actor The Actor object.
* #return {string} Returns actor information, or the type of actor if not a user.
*/
function getActorInfo(actor) {
if ('user' in actor) {
return getUserInfo(actor.user);
}
return getOneOf(actor);
}
/**
* #param {object} target The Target object.
* #return {string} Returns the type of a target and an associated title.
*/
function getTargetInfo(target) {
if ('driveItem' in target) {
const title = target.driveItem.title || 'unknown';
return 'driveItem:"' + title + '"';
}
if ('drive' in target) {
const title = target.drive.title || 'unknown';
return 'drive:"' + title + '"';
}
if ('fileComment' in target) {
const parent = target.fileComment.parent || {};
const title = parent.title || 'unknown';
return 'fileComment:"' + title + '"';
}
return getOneOf(target) + ':unknown';
}
Final goal: to get the amount of days I work for all my clients.
That a idea
After some search and code (some help with GTP so sorry for begin code)
function listDriveActivity(sheet,timezone) {
let pageToken = null;
do {
try {
// Activity.query method is used Query past activity in Google Drive.
const response = DriveActivity.Activity.query({ "ancestor_name": "items/13oHhdSDQqnM4ppO48FPJq7HAmVjR27H5", //"items/root",
"filter": "time >= \"2022-01-01T00:00:00Z\" time < \"2022-01-31T00:00:00Z\" detail.action_detail_case:EDIT",
"consolidation_strategy": { "legacy": {} },"pageSize": 10,pageToken: pageToken});
//Logger.log(response);
const activities = response.activities;
if (!activities || activities.length === 0) {
console.log('No activity.');
return;
}
//console.log('Recent activity:');
for (const activity of activities) {
// get time information of activity.
const time = getTimeInfo(activity);
// get the action details/information
const action = getActionInfo(activity.primaryActionDetail);
// get the actor's details of activity
//const actors = activity.actors.map(getActorInfo);
// get target information of activity.
const targets = activity.targets.map(getTargetInfo);
// print the time,actor,action and targets of drive activity.
// const folderName = activity.targets.map(getFileArborescenceByName);
const folderName = activity.targets.map(getFileArborescenceByID);
var NomDossier = JSON.stringify(folderName).replace(/\[\"|\"\]/g,'');
const ClientName = getClient(NomDossier);
//console.log('%s: %s, %s, %s, %s', time, action, targets, folderName, ClientName);
const timeAsDate = new Date(time);
const lastModified = Utilities.formatDate(timeAsDate, "UTC", "dd-MM-yyyy HH:mm");
var Nomfichier = JSON.stringify(targets).replace(/\[\"|\"\]/g,'');
sheet.appendRow([lastModified, action, Nomfichier, NomDossier, ClientName]);
}
pageToken = response.nextPageToken;
}
catch (err) {
// TODO (developer) - Handle error from drive activity API
console.log('Failed with an error %s', err.message);
}
} while (pageToken);
return sheet;
}
function getFileArborescenceByID(activity) {
if ('driveItem' in activity) {
try {
const fileName = activity.driveItem.name;
var modif = getLastPart (fileName);
var file = DriveApp.getFileById(modif);
var folders = [];
var parent = file.getParents();
while (parent.hasNext()) {
parent = parent.next();
folders.push(parent.getName());
parent = parent.getParents();
}
if (folders.length) {
// Display the full folder path
var folderName = (folders.reverse().join("/"));
}
//var ClientName = getClient(folderName);
}
catch (err) {
// TODO (developer) - Handle error from drive activity API
console.log('Failed with an error %s', err.message);
}
}
//return 'folderName:"' + folderName + '", Client :"' + ClientName + '"';
return folderName;

Exception: Cannot retrieve the next object: iterator has reached the end

im trying to get files in different folders and get some cell values from them and put it in a sheet.
but i get below error
Exception: Cannot retrieve the next object: iterator has reached the end
im using below code and when i run it. it returns error on line 14
it seems code returns some values from first and second folders but for third one it returns error
after running macro it gives log below:
5:24:43 PM Notice Execution started
5:24:45 PM Info [[]]
5:24:45 PM Info [[]]
5:24:46 PM Error
Exception: Cannot retrieve the next object: iterator has reached the end.
list_files_in_folders # macros.gs:14
my code : line 14 error
function list_files_in_folders(){
var sh = SpreadsheetApp.getActiveSheet();
var mainfolder = DriveApp.getFolderById('id-here'); // I change the folder ID here
var mfolders = mainfolder.getFolders();
var data = [];
data.push(['Person Name','File Name','Value 1','Value 2']);
while (mfolders.hasNext){
var mfolder = mfolders.next();
var personfolder = DriveApp.getFolderById(mfolder.getId());
var pfiles = personfolder.getFiles();
data.push([personfolder.getName,,,]);
while(pfiles.hasNext){
var pfile = pfiles.next(); //error here
var personfile = SpreadsheetApp.openById(pfile.getId());
var value1 = personfile.getSheetValues(2,9,1,1);
//var value2 = personfile.getSheetValues();
Logger.log(value1);
data.push(["",pfile.getName,value1,""]);
}
}
sh.getRange(1,1,data.length,data[0].length).setValues(data);
}
Folder or File hasNext() is a method, not a property.
Replace both
mfolders.hasNext
pfiles.hasNext
With
mfolders.hasNext()
pfiles.hasNext()
Reference
FolderIterator.hasNext()
FileIterator.hasNext()
You can make your list_files_in_folders() function easier to adapt by doing report building in one function and the recursive subfolder iteration in another, using closures to process the files.
More lines of code are required, but making changes becomes much easier, and you can apply these functions in other use cases more smoothly — just modify the _fileAction and _folderAction closures as required.
This pattern also lets you handle errors and timeouts more gracefully.
/** #NotOnlyCurrentDoc */
'use strict';
function list_files_in_folders() {
const mainFolder = DriveApp.getFolderById('...put folder ID here...');
const data = generateReport_(mainFolder);
SpreadsheetApp.getActiveSheet().getRange('A1')
.offset(0, 0, data.length, data[0].length)
.setValues(data);
}
/**
* Generates a report based on spreadsheets organized by subfolder.
*
* #param {DriveApp.Folder} folder A folder with files and subfolders.
* #return {String[][]} A 2D array that contains a report of files organized by subfolder.
*/
function generateReport_(folder) {
const data = [];
const _errorHandler = (error) => console.log(error.message);
const _prefix = (folderName, nestingLevel) => ' '.repeat(nestingLevel) + folderName;
const _folderAction = (folder, nestingLevel) => {
data.push([_prefix(folder.getName(), nestingLevel), '', '', '']);
};
const _fileAction = (file) => {
const ss = getSpreadsheetFromFile_(file, _errorHandler);
if (ss) {
data.push(['', ss.getName(), ss.getSheetValues(2, 9, 1, 1), '']);
} else {
data.push(['', file.getName(), '(not a Google Sheet)', '']);
}
};
const timelimit = new Date().getTime() + 4 * 60 * 1000; // stop when 4 minutes have passed
const error = processFilesInFolderRecursively_(folder, _folderAction, _fileAction, _errorHandler, 1, timelimit);
if (error) {
data.push([error.message, '', '', '']);
_errorHandler(error);
}
return [['Person Name', 'File Name', 'Value 1', 'Value 2']].concat(
data.length ? data : [['(Could not find any files. Check folder ID.)', '', '', '']]
);
}
/**
* Iterates files in a folder and its subfolders, executing
* _folderAction with each folder and _fileAction with each file.
*
* #param {DriveApp.Folder} folder The folder to process.
* #param {Function} _folderAction The function to run with each folder.
* #param {Function} _fileAction The function to run with each file.
* #param {Function} _errorHandler The function to call when an error occurs.
* #param {String} nestingLevel Optional. A number that indicates the current folder nesting nevel.
* #param {Number} timelimit Optional. The moment in milliseconds that indicates when to stop processing.
* #return {Error} An error when the function ran out of time, otherwise undefined.
* #license https://www.gnu.org/licenses/gpl-3.0.html
*/
function processFilesInFolderRecursively_(folder, _folderAction, _fileAction, _errorHandler, nestingLevel, timelimit) {
// version 1.2, written by --Hyde, 1 December 2022
nestingLevel = nestingLevel || 0;
const outOfTime = new Error('Ran out of time.');
if (new Date().getTime() > timelimit) {
return outOfTime;
}
const files = folder.getFiles();
while (files.hasNext()) {
if (new Date().getTime() > timelimit) {
return outOfTime;
}
try {
_fileAction(files.next());
} catch (error) {
_errorHandler(error);
}
}
const subfolders = folder.getFolders();
while (subfolders.hasNext()) {
const folder = subfolders.next();
_folderAction(folder, nestingLevel);
const error = processFilesInFolderRecursively_(folder, _folderAction, _fileAction, _errorHandler, nestingLevel + 1, timelimit);
if (error) {
return error; // outOfTime
};
}
}
/**
* Gets a spreadsheet object from a file object.
*
* #param {DriveApp.File} file The file that contains a spreadsheet.
* #param {Function} _errorHandler The function to call when an error occurs.
* #return {SpreadsheetApp.Spreadsheet} The spreadsheet object, or null if file is not a spreadsheet or cannot be accessed.
* #license https://www.gnu.org/licenses/gpl-3.0.html
*/
function getSpreadsheetFromFile_(file, _errorHandler) {
// version 1.0, written by --Hyde, 9 October 2022
if (file.getMimeType() !== 'application/vnd.google-apps.spreadsheet') {
return null;
}
let ss;
try {
ss = SpreadsheetApp.open(file);
} catch (error) {
_errorHandler(error);
return null;
}
return ss;
}

Retrieve all my subscriptions in Google Sheets

I'm using YouTube Data API v3 and Google Apps Script for retrieve all my subscriptions.
The problem I'm facing is that - using the following code, the response brings duplicated channels:
do {
const mySubsResponse = YouTube.Subscriptions.list('snippet', {
mine: true,
//channelId: "<MY_CHANNEL_ID>",
maxResults: 50,
fields: "pageInfo(totalResults),nextPageToken,items(snippet(title,resourceId(channelId)))"
});
if (!mySubsResponse || mySubsResponse == undefined) {
Logger.log('No subscriptions found.');
SpreadsheetApp.getUi().alert("No subscriptions found.");
break;
}
// Loop all my subscriptions found in the response:
for (let j = 0; j < mySubsResponse.items.length; j++) {
const mySubItem = mySubsResponse.items[j];
sheet.getRange("H" + incrSub).setValue(mySubItem.snippet.title);
sheet.getRange("I" + incrSub).setValue(mySubItem.snippet.resourceId.channelId);
incrSub++;
}
nextPageToken = mySubsResponse.nextPageToken;
} while (nextPageToken);
I believe this is due each item in the response is actually the video uploaded by the channel I'm subscribed to - I don't think it's a problem with the page token.
In the code above, I've commented the channelId parameter and I've testted with both: mine:true and channelId:<MY_CHANNEL_ID> and, the totalResults shows me I have 479 subscriptions, but, when I'm looping the results,
For example, I'm subscribed to the channel called "Channel_1"; this
channel had uploaded three videos today. The response of the code
above brings me "Channel_1" three times, when it should be only 1 -
because I'm subscribed to "Channel_1" once.
What I want to get is a list of all channels I'm subscribed to.
I've checked the subscriptions:list documentation, but, it's not clear how I can get my subscriptions only.
If the subscriptions:list endopint is not the correct one for this task, which endpoint enables me to bring the desired results?1
1a list of all channels I'm subscribed to.
After checking more closely (and, I admit, after a little break I have), I finally found the problem and the solution:
The problem is: I wasn't using the nextPageToken in every loop, so, basically, I was requesting the same page without actually making any pagination.
In this section:
const mySubsResponse = YouTube.Subscriptions.list('snippet', {
mine: true,
//channelId: "<MY_CHANNEL_ID>",
maxResults: 50,
fields: "pageInfo(totalResults),nextPageToken,items(snippet(title,resourceId(channelId)))"
});
Can be seen that the pageToken: nextPageToken is not defined.
Then, the solution is:
Modify the code for sending the nextPageToken obtained.
This is the modified code:
// Call my subscriptions:
/** Token pagination. */
var nextPageToken = "";
/** Row position where to start writing the results. */
var incrSub = 6;
/**
* Get all my subscriptions.
*/
do {
const mySubsResponse = YouTube.Subscriptions.list('snippet', {
channelId: "<MY_CHANNEL_ID>", // also works with "mine: true".
maxResults: 50,
// Here, the first time the call is made, the "nextPageToken" value
// is empty. In every iteration (if "nextPageToken" is retrieved),
// the "nextPageToken" is used - in order to get the next page.
pageToken: nextPageToken,
fields: "nextPageToken,items(snippet(title,resourceId(channelId)))"
});
if (!mySubsResponse || mySubsResponse == undefined) {
Logger.log('No subscriptions found.');
SpreadsheetApp.getUi().alert("No subscriptions found.");
break;
}
// Write the subscriptions returned in the response:
for (let j = 0; j < mySubsResponse.items.length; j++) {
const mySubItem = mySubsResponse.items[j];
sheet.getRange("H" + incrSub).setValue(mySubItem.snippet.title);
sheet.getRange("I" + incrSub).setValue(mySubItem.snippet.resourceId.channelId);
incrSub++;
}
// Check the token:
try {
if (mySubsResponse.nextPageToken != null || mySubsResponse.nextPageToken != undefined) {
nextPageToken = mySubsResponse.nextPageToken;
} else {
nextPageToken = undefined;
break;
}
} catch (ex_page) {
// An error occurred. Check closely the code.
}
} while (nextPageToken != undefined);
With this modified code, all of my subscriptions are returned successfully.

trouble with "statistics" property when using with YouTube.Search.list in Google App Script

I cannot seem to get this script to work for 'statistics':
function searchByKeyword2() {
var results = YouTube.Search.list('statistics', {q: 'dogs', maxResults: 5, });
for(var i in results.items) {
var item = results.items[i];
Logger.log(item);
}
}
I can use 'id', 'snippet', or 'id, snippet', but I cannot get it to work with 'statistics'. I've been looking an answer for hours, but I haven't found anything. Any clues?
Per the API documentation, YouTube.Search includes results for Videos, Channels, and Playlists. Not all of these resources have statistics nodes, and thus the YouTube.Search endpoint does not allow querying for the statistics node - only id and snippet.
For collections which track statistics, like Videos, you query them directly to access statistics. Since Videos.list does not search, you need to first search and then provide the relevant video IDs. Note that you can change the search order (and many other search properties) - full details available in the API reference - but the default sort is 'relevance'.
As an example:
function getVideoStatistics(videoIds) {
const options = {
id: videoIds.join(","),
fields: "nextPageToken,items(id,statistics)"
};
const results = [];
do {
var search = YouTube.Videos.list('statistics', options);
if (search.items && search.items.length)
Array.prototype.push.apply(results, search.items);
options.pageToken = search.nextPageToken;
} while (options.pageToken);
return results;
}
function getVideosFromQuery(query, maxResults) {
const options = {
q: query,
maxResults: maxResults,
type: 'video',
fields: "nextPageToken,pageInfo/totalResults,items(id/videoId,snippet(title,channelTitle))"
};
const results = [];
do {
var search = YouTube.Search.list('snippet', options);
if (search.items && search.items.length)
Array.prototype.push.apply(results, search.items);
options.pageToken = search.nextPageToken;
} while (options.pageToken && results.length < search.pageInfo.totalResults && results.length < maxResults);
return results;
}
function foo() {
var someQuery = "something";
var searchResults = getVideosFromQuery(someQuery, 50);
var ids = searchResults.map(function (videoSearchResult) {
return videoSearchResult.id.videoId;
});
var stats = getVideoStatistics(ids);
console.log({message:"query video statistics", searchResults: searchResults, statistics: stats});
}

How to get permissionId in Drive API v3?

I want to delete permission from the file.
In Drive API v2,
PermissionId permissionId = service.permissions().getIdForEmail(account).execute();
service.permissions().delete(fileId, permissionId.getId()).execute();
But According to document, permissions().getIdForEmail(String email) is removed and alternate is nothing.
https://developers.google.com/drive/v3/web/migration
I can't find how to get permissionId from specific Email address in drive API v3.
Do you have any idea?
I found a simple solution:
PermissionList permissions = service.permissions().list(sharedFolderId).setFields("nextPageToken, permissions(id,emailAddress)").execute();
for (Permission p : permissions.getPermissions()) {
if (p.getEmailAddress().equals(adresseEmail)) {
service.permissions().delete(sharedFolderId, p.getId()).execute();
}
}
Two years later, but your question was the first result I found when searching for a solution. I found a workaround and I hope this will help others with the same issue. This is what I did to get the permission id:
this.getPermissionId = function(emailAddress) {
return new Promise((resolve, reject) => {
const input = {
q: '"' + emailAddress + '" in writers or "' + emailAddress + '" in readers',
fields: 'files(permissions)',
pageSize: 1
};
const request = gapi.client.drive.files.list(input);
request.execute(result => {
if(result.error) {
reject(result.error);
} else if(result.files && result.files[0] && result.files[0].permissions && result.files[0].permissions[0]) {
const permissions = result.files[0].permissions;
let permissionId;
permissions.forEach(permission => {
if(permission.emailAddress == emailAddress) {
permissionId = permission.id;
}
});
if(permissionId) {
resolve(permissionId);
}
else {
reject('permissionIdUndefined');
}
}
});
})
};
a .NET version that solved my needs
public static string GetPermissionIdForEmail(DriveService service, string emailAddress)
{
string pageToken = null;
do
{
var request = service.Files.List();
request.Q = $"'{emailAddress}' in writers or '{emailAddress}' in readers or '{emailAddress}' in owners";
request.Spaces = "drive";
request.Fields = "nextPageToken, files(id, name, permissions)";
request.PageToken = pageToken;
var result = request.Execute();
foreach (var file in result.Files.Where(f => f.Permissions != null))
{
var permission = file.Permissions.SingleOrDefault(p => string.Equals(p.EmailAddress, emailAddress, StringComparison.InvariantCultureIgnoreCase));
if (permission != null)
return permission.Id;
}
pageToken = result.NextPageToken;
} while (pageToken != null);
return null;
}
I have done this code in .NET using C#.
I hope you have already created the drive service using user's access token.
After that this code can help you to get permission ID:
var permissionFile = driveService.About.Get();
permissionFile.Fields = "*";
var perm = permissionFile.Execute();
permissionId = perm.User.PermissionId;
The permissionId will give you the required ID.
I use UrlFetchApp with Google Apps Script to replace Drive API v2 and Advanced Drive Service (based on v2).
With a company domain service account, the section getService(userEmail) uses the library https://github.com/googleworkspace/apps-script-oauth2 to send the request on behalf of userEmail.
/**
* Get user permission Id.
*
* #param {String} userEmail - Email address for About query.
* https://developers.google.com/drive/api/v3/reference/about
*/
function TEST_getIdForEmailV3() { getIdForEmailV3('YourEmail#CompanyDomain.com') }
function getIdForEmailV3(userEmail) {
var service = getService(userEmail);
if (service.hasAccess()) {
var url = 'https://www.googleapis.com/drive/v3/about' + '?fields=user/permissionId';
var options = {
'method': 'get',
'contentType': 'application/json',
'headers': { Authorization: 'Bearer ' + service.getAccessToken() }
};
var response = UrlFetchApp.fetch(url, options);
var resultParsed = JSON.parse(response.getContentText());
return resultParsed.user.permissionId;
} else {
return 0;
};
}