Alexandr Alexandr - 4 months ago 23
Linux Question

What to write in a body of a request to GoogleDrive API?

I am trying to work with GoogleDrive via Linux console but I have problems with requests which require a body. All permissions are given. All other requests (without body) are working.

If I want to create a folder in the GoogleDrive it shows me an error.

wget --header="Host: www.googleapis.com" --header="Authorization: Bearer MY_TOKEN" --header="Content-Length: 18" --header="Content-Type: application/vnd.google-apps.folder" --method=POST --body-data='{"title":"TemDir"}' 'https://www.googleapis.com/upload/drive/v2/files' -O result.html


Response is:

--2016-07-07 15:03:34-- https://www.googleapis.com/upload/drive/v2/files
Resolving www.googleapis.com (www.googleapis.com)... 209.85.233.95, 2a00:1450:4010:c08::5f
Connecting to www.googleapis.com (www.googleapis.com)|209.85.233.95|:443... connected.
HTTP request sent, awaiting response... 400 Bad Request
2016-07-07 15:03:35 ERROR 400: Bad Request.


When I send requests without a body they all work:

List first 3 files:

wget --header="Host: www.googleapis.com" --header="Authorization: Bearer MY_TOKEN" --method=GET 'https://www.googleapis.com/drive/v2/files?orderBy=createdDate&maxResults=3' -O result.html


Upload a video file:

wget --header='Host: www.googleapis.com' --header='Content-Type: video/mp4' --header='Content-Length: 9356131' --header='Authorization: Bearer MY_TOKEN' --post-file=test.mp4 https://www.googleapis.com/upload/drive/v2/files


Please, help me resolve this issue

Answer

You need to use correct syntax. Here is the working one for /v2. Though I will recommend you to move to /v3 as soon. This will create a folder of given name.

Using version /v2

wget  --header="Authorization: Bearer MY_TOKEN"  --header="Content-Type: application/json" --method=POST --body-data='{"title":"TemDir121", "mimeType": "application/vnd.google-apps.folder"}' 'https://www.googleapis.com/drive/v2/files' -O result.html

for /v3

wget  --header="Authorization: Bearer MY_TOKEN"  --header="Content-Type: application/json" --method=POST --body-data='{"name":"TemDir", "mimeType": "application/vnd.google-apps.folder"}' 'https://www.googleapis.com/drive/v3/files' -O result.html
Comments