1

There is some api endpoint, which i try to send POST request with code like this

import requests
url = 'https://deviantart.com/api/v1/oauth2/collections/folders/create'
headers = {'Content-Type': 'application/json'}
data = {'folder': 'folder_name'}
params = {'access_token': '<authorization_code_flow_token>'}
r = requests.post(url=url,
                  headers=headers,
                  params=params,
                  json=data)
print(r.text)

But i get 400 response:

{
  "error":"invalid_request",
  "error_description":"Request field validation failed.",
  "error_details":{"folder":"folder is required"},
  "status":"error"
}

I don't understand why it fails, because their example with curl works fine.

curl https://www.deviantart.com/api/v1/oauth2/collections/folders/create \
-d "folder=Awesome Collection" \
-d access_token=Alph4num3r1ct0k3nv4lu3

And I was successful with post responses to get authenticated.
I tried to change content-type header(json and x-www-formurlencoded) and pass data-payload different ways(passing json string to data param, passing dict to json, passing paylod as query string). But It does not work. I dont have a clue what i am doing wrong. It seems like i send payload wrong or put wrong headers, but i tried a lot of "combinations" and still no effect.

For next hour you if you want try to help you can use access_token: ba4550889c8c36c8d82093906145d9fd66775c959030d3d772

Archirk
  • 427
  • 7
  • 25

1 Answers1

1

The following code will work.

import requests

url = "https://www.deviantart.com/api/v1/oauth2/collections/folders/create"

payload={'folder': 'Awesome Collection',
'access_token': 'ba4550889c8c36c8d82093906145d9fd66775c959030d3d772'}
files=[]
headers = {}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)
Shreyansh
  • 458
  • 4
  • 11
  • For some reason it does, but why? For all GET and other POST requests i use no custom headers at all, and pass access_token as url query param. Why just Authorization header solve it while they don't pass any additional data or headers in curl – Archirk Jun 26 '21 at 21:20
  • Updated the code, the server here is accepting token both as a parameter and also in the headers, that's why my prior solution worked. – Shreyansh Jun 26 '21 at 21:31
  • e.g. in this endpoint they pass token in url query. https://www.deviantart.com/developers/http/v1/20210526/collections_folders/35030a41b12db6c4edf6bf9b163e4327 . So for me it is not obvious why should i pass token to header. About data/json param you probably right, but i used json argument since i was sure they expect json string and seems like python-requests make this kwarg for thath – Archirk Jun 26 '21 at 21:31
  • I have updated the solution without using the headers, but it is recommended to pass the token in headers. – Shreyansh Jun 26 '21 at 21:32