Revisions list and get missing information - google-drive-api

Using the Google APIs Explorer as well as the official C# library for Google Drive API v3, the response is missing most of the documented fields. I specifically need access to who did the revisions, which should be accessible via $.lastModifyingUser.
I've tried this on my domain admin account as well as a service account, so it shouldn't be a permission issue, at least not that I can see. What am I doing wrong, if anything?
List:
{
"kind": "drive#revisionList",
"revisions": [
{
"kind": "drive#revision",
"id": "1",
"mimeType": "application/vnd.google-apps.spreadsheet",
"modifiedTime": "2016-11-16T18:29:29.527Z"
},
{
"kind": "drive#revision",
"id": "14",
"mimeType": "application/vnd.google-apps.spreadsheet",
"modifiedTime": "2016-11-16T18:35:49.184Z"
},
...
Single get:
{
"kind": "drive#revision",
"id": "134088",
"mimeType": "application/vnd.google-apps.spreadsheet",
"modifiedTime": "2017-05-24T11:48:00.788Z"
}

You need to add fields="*", or fields="permissions(id,lastModifyingUser)" to your request to instruct Drive to return the full resource. By default, only a minimal set of properties is returned.

I was not able to retrieve the "lastModifyingUser" from the revisions API but I was able to retrieve the "lastModifyingUser" from the Files.get fro example:
File foundFile = service.files().get(id)
.setSupportsTeamDrives(true)
.setFields("parents, webViewLink, properties, lastModifyingUser")
.execute();
System.out.println("Found file is " + foundFile);
System.out.println("Parents " + foundFile.getParents());
System.out.println("Owners " + foundFile.getOwners());
System.out.println("Props " + foundFile.getProperties());
System.out.println("Last user " + foundFile.getLastModifyingUser());

Related

Google drive api Files: get doesn't list full metadata

I tried using the example get method on the google drive api documentation, however what is returned is not the full metadata of the file, only this:
"kind": "drive#file",
"id": "1vbLiXALYOYoVev1KD_ajVBfh5_CgvGgP",
"name": "3.png",
"mimeType": "image/png",
"result": {
"kind": "drive#file",
"id": "1vbLiXALYOYoVev1KD_ajVBfh5_CgvGgP",
"name": "3.png",
"mimeType": "image/png"
}
}
What should appear is something with many more fields:
{
"kind": "drive#file",
"id": "1vbLiXALYOYoVev1KD_ajVBfh5_CgvGgP",
"etag": "\"MTU4Njg3NTU3MjUxOQ\"",
"selfLink": "",
"webContentLink": "",
"alternateLink": "",
"embedLink": "",
"iconLink": "",
"thumbnailLink": "",
"title": "3.png"
// ... and so on
I get the full response when I use the "try api" screen on the api docs, but not when calling it from javascript:
function printFile(fileId) {
appendPre(fileId)
var request = gapi.client.drive.files.get({
'fileId': fileId
});
request.execute(function(resp) {
console.log(JSON.stringify(resp, null, 2))
});
}
I used the https://www.googleapis.com/auth/drive.file scope, which is listed as a scope sufficient for the request to be completed. Help!
How about this modification?
At Drive API V3, when fields property is not used, a part of fields is returned. So in your case, for example, you can retrieve other parameters using fields: "*". When your script is modified, please modify as follows.
From:
var request = gapi.client.drive.files.get({
'fileId': fileId
});
To:
var request = gapi.client.drive.files.get({
'fileId': fileId,
'fields': "*" // Added
});
Reference:
Parameters of Files: get

Set Updated by when uploading files to BIM360 Docs with 2-legged authentification

I am building a tool automatically scan a local folder and upload files to BIM360 Docs, with 2 legged authentification, so far files can already uploaded to BIM360 Docs using Forge Data Management API however the "updated by" field is empty. what i want to accomplish is load the current login user and set the current user's name as updated by.
I cannot find anywhere i can use from Forge API to set the "updated by", can anyone suggest?
Thanks a lot.
When you post/patch an item/version there are two fields you can specify that suit your needs - lastModifiedUserId and lastModifiedUserName:
"attributes": {
"mimeType": "appplication/pdf",
"displayName": "version-test.pdf",
"name": "version-test.pdf",
"extension": {
"data": {
"tempUrn": null,
"storageType": "OSS",
"properties": {},
"storageUrn": "urn:adsk.objects:os.object:wip.dm.prod/3c8f6bbc-fe5c-4815-a92e-8b8635e7b1cb.pdf"
},
"version": "1.0",
"type": "versions:autodesk.bim360:FileVersions",
"schema": {
"href": "/schema/v1/versions/versions%3Aautodesk.bim360%3AFileVersions-1.0"
}
},
"createUserName": "John Doe",
"createTime": "2016-04-01T11:09:03.000Z",
"createUserId": "BW9RM76WZBGL",
"lastModifiedUserName": "John Doe",
"lastModifiedUserId": "BW9RM76WZBGL",
"versionNumber": 2,
"lastModifiedTime": "2016-04-01T11:11:18.000Z"
},
"type": "versions",
"id": "urn:adsk.wipprod:fs.file:vf.d34fdsg3g?version=2",
"links": {
"self": {
"href": "/data/v1/projects/b.6f8813fe-31a7-4440-bc63-d8ca97c856b4/versions/urn%3Aadsk.wipprod%3Afs.file%3Avf.b909RzMKR4mhc3O7UBY_8g%3Fversion%3D1"
}
}
Normally, we would suggest you to use 3 legged token to modify a file in BIM360 Docs, because that will keep track the users who made the modification to that file(updated by). Yes you can also use 2 legged token for file management in BIM360 for now, but this behaviour may change in the future, and we will clearly mention in our blog if it happens.
There is no user information for any changes to the BIM360 files with 2 legged token, but there is one way, follow the Upload Files to BIM 360 Document Managemen Tutorial, in Step 7 & 8, add the x-user-id parameter in the header as the following picture, you will see the last modified user is filled.
Check this https://forge.autodesk.com/en/docs/bim360/v1/reference/http/users-GET/ if you want to know how to get the x-user-id information, should be uid(Autodesk ID).

Using a local Open API Standard file to to create an ARM template for a web service

I am working on an old web service where I generate the rest endpoints documentation that comply with OAS standards using a custom tool. Using this OAS json file I can deploy the API to Azure API Managements services through the portal and it all works fine. However, I need to automate this process and hence need to use ARM templates to deploy all web services to Azure APIM. I have been looking into the examples provided https://learn.microsoft.com/en-us/azure/templates/microsoft.apimanagement/service/apis but just can't seem to wrap my head around how to use a local OAS.json file or a file in github.
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for all resources."
}
}
},
"variables": {
"apiManagementServiceName": "price-capture"
},
"resources": [
{
"apiVersion": "2018-01-01",
"type": "Microsoft.ApiManagement/service/apis",
"name": "[variables('apiManagementServiceName')]",
"properties": {
"displayName": "Service display Name",
"apiRevision": "1",
"description": "API description",
//need help since it's not a swagger url
//wondering if there is a way to ref a local file like the option
//provided in the portal when we register api's manually.
"serviceUrl": "----",
"path": "----",
"protocols": [
"https"
],
"isCurrent": true,
"apiVersion": "v1",
"apiVersionDescription": "apiVersionDescription"
}
}
]
}
You can deploy and configure an entire API on API Management via ARM templates, but you cannot use a local file to provide the OpenApi/Swagger.
In your case the OpenApi/Swagger needs to be publicly accessible so the resource manager can read from it, so if the Github URL is freely accessible it should work.
I typically store the OpenApi/Swagger to a storage account and use the SAS token to access it from the ARM template.
You can check out this blog for details on automating API deployment in APIM:
https://blog.eldert.net/api-management-ci-cd-using-arm-templates-linked-template/
You can deploy the API using an Azure Resource Manager template of type Microsoft.ApiManagement/service/apis, and to use an Open API / swagger definition you need to specify the contentValue and and contentFormat parameters of the template
{
"name": "awesome-api-management/petstore",
"type": "Microsoft.ApiManagement/service/apis",
"apiVersion": "2018-06-01-preview",
"properties": {
"path": "petstore"
"contentValue": "petstore swagger file contents here", // or it's URL
"contentFormat": "swagger-json", // or swagger-link-json if externally available
}
}
I don't think it's possible to deploy the APIs configs via templates.
I've been trying to figure this out myself but I'm pretty sure you can't include the actual APIs you want in the service.
From what I can tell, you can't do that with the GIT repo either because that needs authentication that is manually created in the portal
I think the only thing you can automate with the ARM template is the actual API Management service and then you need to use the Azure API to add and configure the APIs on it.
However, I have yet to figure out how to do that myself.
I actually have a service ticket open to get help on that.
The API has changed slightly so this works:
The yaml file (calculatorApiFile) needs to be uploaded first to a blob storage, but this can be done as part of the deployment pipeline
{
"type": "Microsoft.ApiManagement/service/apis",
"apiVersion": "2019-01-01",
"name": "[concat(parameters('service_name'), '/b12b1d5ab8204cg6b695e3e861fdd709')]",
"dependsOn": [
"[resourceId('Microsoft.ApiManagement/service', parameters('service_name'))]"
],
"properties": {
"displayName": "Calculator",
"apiRevision": "1",
"description": "A simple Calculator ",
"path": "calc",
"value": "[concat(parameters('containerUri'), parameters('calculatorApiFile'), parameters('containerSasToken'))]",
"format": "openapi-link",
"protocols": [
"https"
],
"isCurrent": true
}
}
I figured out the answer ..all I had to do was write an azure function that fetches the oas.yaml file from a private github repository.
"variables":{
"swagger_json":"[concat(parameters('url_of_azurefunctionwithaccesskey'),'&&githuburi='parameter('raw_url'),'&githubaccesstoken=',parameter('personalaccesstoken')]"
},
"resources": [
{
"type": "Microsoft.ApiManagement/service/apis",
"name": "[concat(parameters('apimName') ,'/' ,parameters('serviceName'))]",
"apiVersion": "2018-06-01-preview",
"properties": {
"apiRevision": "[parameters('apiRevision')]",
"path": "pricecapture",
"contentValue": "[variables('swagger_json')]",
"contentFormat": "openapi-link"
}
}]
The Azure function that I had to write was something like this:
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.IO;
using System.Text;
public static async Task<HttpResponseMessage> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
var gitHubUri = req.Query["githuburi"];
var gitHubAccessToken = req.Query["githubaccesstoken"];
var encoding = Encoding.ASCII;
if (string.IsNullOrEmpty(gitHubUri))
{
var errorcontent = new StringContent("please pass the raw file content URI (raw.githubusercontent.com) in the request URI string", Encoding.ASCII);
return new HttpResponseMessage
{
StatusCode = HttpStatusCode.BadRequest,
Content = errorcontent
};
}
else if (string.IsNullOrEmpty(gitHubAccessToken))
{
var errorcontent = new StringContent("please pass the GitHub personal access token in the request URI string", Encoding.ASCII);
return new HttpResponseMessage
{
StatusCode = HttpStatusCode.BadRequest,
Content = errorcontent
};
}
else
{
var strAuthHeader = "token " + gitHubAccessToken;
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3.raw");
client.DefaultRequestHeaders.Add("Authorization", strAuthHeader);
var response = await client.GetAsync(gitHubUri);
return response;
}
}
If you load your YAML into a variable, that can be passed to the ARM template and be passed as the value:
deploy.bat:
SETLOCAL EnableDelayedExpansion
set API_DEPLOYMENT=<deployment name>
set API_GROUP=<deployment group>
set API=<api file path.yml>
set OPENAPI=
for /f "delims=" %%x in ('type %API%') do set "OPENAPI=!OPENAPI!%%x\n"
call az deployment group create -n %API_DEPLOYMENT% -g %API_GROUP% --mode Complete -f deploy.json -p openApi="!OPENAPI!"
ENDLOCAL
deploy.json (note the use of replace)
...
{
"type": "Microsoft.ApiManagement/service/apis",
"apiVersion": "2020-12-01",
"name": "[variables('apiName')]",
"properties": {
"path": "[variables('service')]",
"apiType": "http",
"displayName": "[variables('apiDisplayName')]",
"format": "openapi",
"value": "[replace(parameters('openApi'), '\\n', '\n')]"
},
...
},
...

How to export google docs revision with v3 drive api?

How can I export specific version of Google document?
I want to create an exact slice copy of user's gdrive.
The idea is
Get list of head revisions for all files in drive
Download or export every file with exact revision
But for google docs files I can't find an head revision field in /files/list/:
{
"id": "1Vsg1U58vXPCGqL2KFAT-cY9ckAIwWxQQ0-49HOPtHzI",
"name": "Spreadsheet-test-file-1",
"mimeType": "application/vnd.google-apps.spreadsheet",
"version": "844"
},
and when I list all revisions for this file, I get this:
{
"kind": "drive#revisionList",
"revisions": [
{
"kind": "drive#revision",
"id": "1",
"mimeType": "application/vnd.google-apps.spreadsheet",
"modifiedTime": "2017-07-04T07:21:24.849Z"
},
{
"kind": "drive#revision",
"id": "2",
"mimeType": "application/vnd.google-apps.spreadsheet",
"modifiedTime": "2017-07-04T07:21:24.852Z"
}
]
}
But when I try to download this revision, I get an error Only files with binary content can be downloaded. Use Export with Google Docs files.
I know that API docs says that Google docs can't be downloaded with revisions. But if so, I don't understand how to get the exact slice of the drive?
By the way, I am using the Go library for drive api.

Google Drive API: No Parents with drive.files.get

I am using the Google Drive API. I can get a list of files with the drive.files.list API method. Here is the result of a call to this method:
{
"files": [
{
"id": "1yLJFT2bHhsz1WxJX29mD9DiNz0vLDlBvzJf5GJCcboI",
"mimeType": "application/vnd.google-apps.spreadsheet",
"parents": [
"0Bwy7SlKkxnwQRld6RWljU0Y4TG8"
]
},
{
"id": "0Bwy7SlKkxnwQVXdBRmlPMGNoTmM",
"mimeType": "application/vnd.google-apps.folder",
"parents": [
"0B4jAnSzS-VxlZW9RcDJheU5FYW8"
]
},
{
"id": "0Bwy7SlKkxnwQRld6RWljU0Y4TG8",
"mimeType": "application/vnd.google-apps.folder",
"parents": [
"0Bwy7SlKkxnwQQndiQnYxbUF4SjQ"
]
}]
}
When I request individual files using the drive.files.get API call, I would expect to get the same results as shown in the list above. I do when I request the first and third file listed. For example:
GET https://www.googleapis.com/drive/v3/files/0Bwy7SlKkxnwQRld6RWljU0Y4TG8?supportsTeamDrives=true&fields=id%2CmimeType%2Cparents
Result:
{
"id": "0Bwy7SlKkxnwQRld6RWljU0Y4TG8",
"mimeType": "application/vnd.google-apps.folder",
"parents": [
"0Bwy7SlKkxnwQQndiQnYxbUF4SjQ"
]
}
This is identical to what is shown in the list. But for some reason when I request the second file in the list, the parents list is missing from the result:
GET https://www.googleapis.com/drive/v3/files/0Bwy7SlKkxnwQVXdBRmlPMGNoTmM?supportsTeamDrives=true&fields=id%2CmimeType%2Cparents
Result:
{
"id": "0Bwy7SlKkxnwQVXdBRmlPMGNoTmM",
"mimeType": "application/vnd.google-apps.folder"
}
No parents array. Can anyone explain this behavior? I assumed all files should have a parent.
If you haven't done yet, you may want to check if you have an access to the parent of your second file. As stated in Enabling Team Drives:
A parent does not appear in the parents list if the requesting user is a not a member of the Team Drive and does not have access to the parent.
In Behavioral differences, fields related to permissions and ownership are populated differently.