Google Drive API - List Permissions of a file - google-api

I am using the list permissions endpoint (https://developers.google.com/drive/api/v3/reference/permissions/list) to get all the permissions for a file that exists in a shared drive.
I am running into an issue with a file I have that lives in a shared drive. The file has 100 permissions. The max limit on the number of permissions returned for a shared drive file is 100, so I should only need to make one request to get all the permissions for the file and the API should not return a next page token.
But this is not the behaviour I am experiencing, after the first request I continuously get the same next page token back from the API.
So for the following code below (written in go), I get into an infinite loop, since I continuously get the same next page token back.
var permissionService PermissionsService := getPermissionsService()
fileID := "1234abcd"
nextPageToken := ""
anotherPage := true
permissions := []*Permission{}
for anotherPage {
result, err := permissionService.
List(fileID).
SupportsAllDrives(true).
SupportsTeamDrives(false).
Fields("*").
PageSize(100).
Do()
if err != nil {
panic(err)
}
anotherPage = result.NextPageToken != ""
nextPageToken = result.NextPageToken
permissions = append(permissions, result.Permissions...)
}
fmt.Println("Permissions", permissions)
Am I supposed to account for this case in my code? From the documentation, this is never mentioned so I assume this is not supposed to happen.

It seems that you did not add the PageToken parameter in your permissionService. You are actually requesting the same first 100 results of the permission service.
Not familiar with the language but something like this
for anotherPage {
result, err := permissionService.
List(fileID).
SupportsAllDrives(true).
SupportsTeamDrives(false).
Fields("*").
PageSize(100).
PageToken(nextPageToken).
Do()
if err != nil {
panic(err)
}
anotherPage = result.NextPageToken != ""
nextPageToken = result.NextPageToken
permissions = append(permissions, result.Permissions...)
}
Can you also verify first if the result.NextPageToken will return "" if the file has < 100 results, I used API explorer and Apps Script to access this API and the nextPageToken is not part of the response body when permission list is less than the pageSize.

Related

NextPageToken gives invalid input error, 400 - Google Directory API ChomeDevices

I have to fetch 250K chromebooks from google workspace (Gsuite), I am using Admin Directory API to retrieve JSON data from Google.
The response returns in chunks of 200 records, in the response is included a nextPageToken, I use that next page token to retrieve the next 200 and so on.
After an hour, of using the nextPageToken attached from the previous request, However Google returns with error 400,
{error_code: 400, "message"=>"Invalid Input: CMiJhq7-5ewCEp0BCm737N8GN......"},
Note: This string 'CMiJhq7-5ewCEp0BCm737N8GN......' which google is calling as invalid is the nextPageToken.
Why is this happening? Does nextPageToken expire after 1 hour?
My code snippet:
query_list = {
'maxResults' => 200,
'access_token' => access_token,
'pageToken' => next_page_token
}
HTTParty.get(endpoint_url, query: query_list)
The nextPage token is created when the initial request is sent. This token is used in order to get the next batch of rows from the request.
This token is intended to be used immediately as the data associated with the initial request may be changed if you wait to long.
So yes next page tokens do expire i would actually expect them to expire in a lot less than an hour. I also wonder if the next page token wouldn't just expire after you used it the first time.
If you want to make the same request again i suggest you do that and get new next page tokens built for you after the hour.
I had to change my approach, initially, I fetched 200 chunks from Google API, performed some time-taking processing and then made entries into my database (database-intensive tasks) and then requested the next 200 chunks and so so. After an hour, the last nextpagetoken sent by Google became invalid.
So, now I fetch 200 chunks, save them to my database in JSON format without performing any database-intensive tasks, request the next 200, and so on. I was able to fetch 300K Chromebooks JSON data from google in around 56 Minutes before the nextPageToken became invalid.
I am now processing that JSON data present in my database, without having network overhead or any google API dependency.
I'm encountering error Error 400: Invalid Value, invalid when I tried to use pageToken parameter of the Google Calendar Events API.
The code (in Go) I was using.
call := service.Events.List(calendarID).SingleEvents(true)
events, err := call.Do()
if err != nil {
return err
}
var allEvents []*calendar.Event
allEvents = append(allEvents, events.Items...)
if events.NextPageToken != "" {
nextPageToken := events.NextPageToken
nextCall := service.Events.List(calendarID).SingleEvents(true).PageToken(nextPageToken)
events, err = nextCall.Do()
if err != nil {
return err
}
allEvents = append(allEvents, events.Items...)
}
As illustrated in Paging through lists of resources, the next API call has to be exactly the same as the previous one. Thus, the following code works with the pageToken (where no nextCall is being used).
call := service.Events.List(calendarID).SingleEvents(true)
events, err := call.Do()
if err != nil {
return err
}
var allEvents []*calendar.Event
allEvents = append(allEvents, events.Items...)
if events.NextPageToken != "" {
nextPageToken := events.NextPageToken
call := call.PageToken(nextPageToken)
events, err = call.Do()
if err != nil {
return err
}
allEvents = append(allEvents, events.Items...)
}

Unable to list compute instances in one project

I am trying to get a list of all GCP compute instances in all projects that I have access to. However, there is one project that keeps giving me a "permission denied" answer, and I cannot figure out why...
In order to authenticate, I have created a service account in one of the projects (let's call it sa1, in project_a). Then, at the organization level, I have given that SA the Compute Admin role. And everything works fine, for all of the projects, except one (let's call this non-working one project_x).
Now I am using the computeService.Instances.AggregatedList call, which according to the docs needs the compute.instances.list permission. If I go to IAM&Admin/Policy Troubleshooter and check, I find out that it should work:
Access granted for API call for sa1#project_a, compute.instances.list, projects/project_x.
If this outcome was unexpected, contact your support team for further assistance.
However, if I try to actually run the code, as soon as I hit that project I get:
googleapi: Error 403: Permission denied on resource project project_x, forbidden
The code is written in Go, based on the sample in the docs. The actual call I am using looks like this:
func getInstances ( ctx context.Context, cs *compute.Service, projectName string) {
req := cs.Instances.AggregatedList(projectName)
err := req.Pages(ctx, func(page *compute.InstanceAggregatedList) error {
for name, instancesScopedList := range page.Items {
if len(instancesScopedList.Instances) > 0 {
fmt.Printf("===== %v =====\n", name)
for _, instance := range instancesScopedList.Instances {
fmt.Printf("-- %#v, %#v\n", instance.Name, instance.NetworkInterfaces[0].AccessConfigs[0].NatIP)
}
}
}
return nil
})
if err != nil {
fmt.Println("Error getting list!")
log.Fatal(err)
}
}
I honestly have no idea what could be causing this, and no idea how to troubleshoot further. Can anyone point me in the right direction?
Oh, and if it matters - there are actually no compute engine instances in project_x. But this should not be a problem - I have other projects with no instances, and I simply get an empty list from them.

How to check if couchbase document exists, without retrieving full content, using golang SDK?

In my code I want to do or not to do some actions depending on document with given key existence. But can't avoid additional network overhead retrieving all document content.
Now I'm using
cas, err := bucket.Get(key, &value)
And looking for err == gocb.ErrKeyNotFound to determine document missed case.
Is there some more efficient approach?
You can use the sub-document API and check for the existence of a field.
Example from Using the Sub-Document API to get (only) what you want
:
rv = bucket.lookup_in('customer123', SD.exists('purchases.pending[-1]'))
rv.exists(0) # (check if path for first command exists): =>; False
Edit: Add go example
You can use the sub-document API to check for document existence like this:
frag, err := bucket.LookupIn("document-key").
Exists("any-path").Execute()
if err != nil && err == gocb.ErrKeyNotFound {
fmt.Printf("Key does not exist\n")
} else {
if frag.Exists("any-path") {
fmt.Printf("Path exists\n")
} else {
fmt.Printf("Path does not exist\n")
}
}

Obtaining Calls for Entire Month

I want to page through an entire month's worth of Calls usage in a report. Unfortunately, I'm experiencing random 500 errors from the API.
First I initialize the first page call to /Calls.json
// Set initial baseUrl
urlStr := "https://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/Calls.json"
baseUrl, err := url.Parse(urlStr)
if err != nil {
panic(err)
}
// Build query parameters and URL
v := url.Values{}
v.Set("StartTime>", "2017-08-01")
v.Set("StartTime<", "2017-08-02")
v.Set("To", os.Args[1])
v.Set("PageSize", "1000")
v.Set("Page", "0")
baseUrl.RawQuery = v.Encode()
// Begin recursive call
pageNext(baseUrl)
I then determine if there is a "next page" by checking the response property next_page_uri, if the property is not blank then I proceed to recursively call the API with the previous calls next_page_uri query.
Each time I end up with a 500 error, sometimes within the first call and sometimes it is after 100-ish calls.
Is this really the best way to obtain such a report?

If value is 0 then input value will be empty, value otherwise

Problem:
I have created a simple form, where there's an input field "num". After submission I want to show the value of num in the same input field, in other words want to retain the input in that field. If the value was set to 0 then I want to ignore that.
I can do it in several languages but I'm not sure about how to do it in Golang. My current template file has,
<input type="text" placeholder="foo" name="bar" value="{{if gt .N 0 }} {{.N}} {{end}} "/>
Server file contains:
data := &listOfReport {
R: r,
I: i,
N: n
}
listTmpl := template.Must(template.New("list_tmpl").Parse(string(report.Template["xxx.tmpl"])))
if err := listTmpl.Execute(w, data); err != nil {
http.Error(w, fmt.Sprintf("Error rendering template %v", err), 500)
}
Another thought is to make N a string so make it '' or value in the server file. But that actually spoils the variable's name/purpose.
Is there any better way to do it? Is ther any better way to access GET parameters directly from template? Please note that the value of N is originally got from a GET variable.
*This code is not tested
There is no standard/builtin way to get any request parameters from within a template, you'll have to put it into your data. (You could write a function which does this for you, but that will result in an ugly hack.)
I don't see what's wrong with your solution.
I take a similar approach, but use structs.
type SignupForm struct {
Name string
Email string
Etcera bool
}
// Type alias
type M map[string]interface{}
...
// In the handler that accepts your form
err := r.ParseForm()
if err != nil {
// handle error
}
signup := SignupForm{}
err := decoder.Decode(signup, r.PostForm)
if err != nil {
// handle error
}
// Store the 'saved' form contents somewhere temporary -
// e.g.
// - cookies (keep in mind the 4K browser limit)
// - server side sessions (Redis; how I do it)
// - db
// In the handler that renders your form
err := template.ExecuteTemplate(w, "form.html", M{
"form": signup,
"csrfToken": csrfToken,
// and so on...
})
Note that wherever you store the form data, make sure it is temporary. Server side sessions are ideal as you can have them expire (if you don't want to delete them manually).

Resources