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

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

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

Related

Curl - implicit grant

Any chance that I can get this OAUTH system to let me login with curl?
When I make the following call with curl, I keep getting the error message bellow and can't get passed it.
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=authorization_code&response_type=code&client_id=xxx_id_xxx&client_secret=xxx_secret_xxx&redirect_uri=https://www.anaf.ro" "https://logincert.anaf.ro/anaf-oauth2/v1/authorize"
{
"error":"invalid_client",
"error_description":"The client app does not support implicit grant"
}
Thanks,
Chris

Unable to upload binary data using python requests

I am trying to translate the following curl command into a python request API call:
curl --header "Content-Type: application/octet-stream" --request PUT --data-binary #content.tar.gz <upload_url>
I have got as far as doing:
import requests
data = open("content.tar.gz", "rb").read()
response = requests.put(
<upload_url>,
headers={"Content-Type": "application/octet-stream"},
data=data
)
Although the status code from the above call is 200 the content.tar.gz file does not seem to get uploaded while the curl command works flawlessly.
I have looked at many different questions regarding translating curl commands to python requests but have not found any reasons why this should not work when the curl command does.
Hope you may be able to give me some pointers on what I am doing wrong.

Spotify API authorization works with cURL but not with python requests

I am trying to get an acess token from the Spotify API. This is the curl command for it :
curl -H "Authorization: Basic M2...xMTJiNDc=" -d "grant_type=authorization_code" -d "code=AQAXvqFp....29kd09" -d "redirect_uri=https%3A%2F%2Fexample.com%2Fcallback" https://accounts.spotify.com/api/token
This works.
However,if I try to run it using python requests,I get a "invalid authorization" token error. I have reproduced the steps to get the auth token multiple times,but still same error.
I read on their GitHub that the auth code works only once,so I used the new auth code with python requests first,but still no luck. This is the python code:
import requests as req
data = {
'grant_type': 'authorization_code',
'code':'AQAXvqFp........9kd09',
'state': '34fFs29kd09=34fFs29kd09',
'redirect_uri': 'https://example.com/callback'
}
header={
"Authorization":f"Basic {authbasic}" #same as in the curl command
}
url = "https://accounts.spotify.com/api/token"
response = req.post(url,headers=header,data=data)
print(response.content)
This is the error:
b'{"error":"invalid_grant","error_description":"Invalid authorization code"}'
How do I resolve this? I am using Python3.6

upload .pcm file using okhttp

I am trying to convert speech to text using Nuance so i am trying to send this request
curl "https://dictation.nuancemobility.net:443/NMDPAsrCmdServlet/dictation?appId=[INSERT YOUR APP ID]&appKey=[INSERT YOUR 128-BYTE STRING APP KEY]&id=C4461956B60B" -H "Content-Type: audio/x-wav;codec=pcm;bit=16;rate=16000" -H "Accept-Language: ENUS" -H "Transfer-Encoding: chunked" -H "Accept: application/xml" -H "Accept-Topic: Dictation" -k --data-binary #audio_16k16bit.pcm
need to upload audio file (.pcm) format.
I am using okhttp3 library following is the builder
RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("file", "audio_16k16bit.pcm", RequestBody.create(MEDIA_TYPE_PNG, "audio_16k16bit"))
.build();
httpBuider.addQueryParameter("appId", "NMDPTRIAL_XXXXXXX_XXX_com20161122071457").addQueryParameter("appKey", "fadaed7b801e10d84272c0a75317d8cee13ab86ae902ab322cd6e1219fcbe79aa5d41526f225fe3497bfdbead6b4b9b7ee7122d773cd0a9fa3ebc042b7a7dc5c");
Request request = new Request.Builder().addHeader("Content-Type","audio/x-wav;codec=pcm;bit=16;rate=16000").addHeader("Accept-Language","eng-GBR").addHeader("Transfer-Encoding","chunked").addHeader("Accept","application/xml").addHeader("Accept-Topic","Dictation").post(requestBody).url(httpBuider.build()).build();
I am getting following log
HTTP ERROR 500
Problem accessing /NMDPAsrCmdServlet/dictation. Reason:
Server Error
Missing anything?
I have experienced the same just now using Jersey. Problem for me was, that Jersey was overwriting the content type header (I tried with Application_Octet_Stream).
Here is the request with which I finally got it to work:
Response myResponse = target.
request().
accept(MediaType.TEXT_PLAIN_TYPE).
header("Accept-Language","DEDE").
header("Accept-Topic", "Dictation").
header("Transfer-Encoding","chunked").
post(Entity.entity(speechStream, "audio/x-wav;codec=pcm;bit=16;rate=16000"));
I suggest using something like fiddler to find out what is really posted and comparing that to the curl post. This is how I finally found out what was wrong with my request.
Remove the header "Transfer-Encoding","chunked". My error was fixed by removing it.

Github API Create Issues return 404 Not found

I am making a request to the below URL-
Post https://api.github.com/repos/kvimal/2048/issues
With my Token as a header for authorization.
The Curl Request
curl -i -X POST https://api.github.com/repos/kvimal/2048/issues -d "{title:'hey'}" -H "Authorization: Bearer xxxxxxxxxxxxxxxxxx" -H "Content-Type: application/json"
And GitHub sends a response 404 Not found. I have reade the Documentation and as far as i have observed i am doing it by the github standards. Can anyone Help with this issues?
As illustrated in this python script, the header should be using 'token' not Bearer'
headers = {
'Content-Type':'application/json',
'Authorization': 'token %s' % token,
}
(That script doesn't use curl, but give an idea of the header)
For curl queries, see this curl POST tutorial:
curl -H "Authorization: token OAUTH-TOKEN"
And the POST message must be complete as well (as in this python script)
issue = {'title': title,
'body': body,
'assignee': assignee,
'milestone': milestone,
'labels': labels}
# Add the issue to our repository
r = session.post(url, json=issue)
(again, not curl, but gives you an example of the body)
Go to "Edit personal access token" page, checkout the "Select scopes" session.
Make sure the token has been granted required access right.
I encountered similar case when trying to create public repo with 'Authorization: token ...' header.

Resources