Web API Requests Authentication - python

I am trying a get request using the following API info:
https://github.com/iblockin/pool_web_api_doc/blob/master/api_en.md
Can someone help me with what the headers should look like, or point out where I am messing up? I keep getting an unauthenticated response.
url = "https://api-prod.poolin.com/api/public/v1/subaccount"
r = requests.get(url, headers = {'authorization': 'accessToken tokenhere'})
Sorry in advance if this has been inevitably answered in 1000 other locations.
Severe noob here, trying to learn

use this
url = "https://api-prod.poolin.com/api/public/v1/subaccount"
r = requests.get(url, headers = {'authorization': 'Bearer tokenhere'})
As mentioned here accessToken should be passed in the headeras
{'authorization':'Bearer TOKEN'} # TOKEN is replaced with the token value to be transmitted
So correct your code accordingly.

Related

Manually solve captcha in a request session

I have a software in python that use post request to reach an API from an external website.
Sometimes, instead of getting the result of my request, I got an URL with a captcha to solve.
Here the code of the requests
headers= {}
headers['Accept']= 'application/json,application/hal+json'
headers['Content-Type']= 'application/json; charset=UTF-8'
headers['Connection']= 'Keep-Alive'
url = 'https://api.website.com/v1/price'
data = [{'category':int(category_id)}]
r = requests.session()
r = requests.post(url, headers=headers, data=json.dumps(data))
So far it's OK, then I get all the date in the following variable and print then in the software using Tkinter:
json_result = json.loads(r.text)
Unfortunately, sometimes the API returns an URL with a link to solve a captcha
{"url":"https://geo.captcha-delivery.com/captcha/?initialCid=AHrlqAAAMAwIhGhOSjsfUAsLEFTg==&cid=Y.M~8XWYwAHts6n_H32O8fm~WTeMw.1cDlRGOH16q4PhWtuo8Xm~KgWeW6d1jQptRljywWkJHFMu9IgEZYRheo3OPww6XjgqXQcs1X1m&referer=https%3A%2F%2Fapi.website.com&hash=05B30BD905598BD2EE8F5A199D973&t=fe&s=575"}
If I open this url in a browser, I can solve the captcha but then I broke the chain so the software is unable to continues.
How could I open in python the url, solve the captcha and when it is solved, continue the request to get the element ?
EDIT : the solution must store the cookie in the request session
Thank you

Put API Request in Python returning error

I'm fairly new to using API's, so bear with me here. I have searched for other problems like this, but haven't encountered any solutions for one that'll help my problem.
Using Postman, I'm able to make a Put request using JSON and it works fine. When I try to use the same JSON body in Python, I am getting this error:
{'code': 'E.Internal', 'error': 'An internal error has occurred processing your request. Please notify Customer Support for assistance.', 'status': 'error'}
The company's customer support is not too helpful so I wanted to see if someone here could help me instead.
Here is my script:
url = 'https://website.com/rest/site/' + record_id
json_body = (JSON body here, same one that works in Postman)
head = {'Accept':'application/json', 'Content-Type': 'application/json'}
response = requests.put(url, auth=(username, password), json=json_body, headers=head)
data = response.json()
print(data)
If I change the requests.put to requests.get and drop everything after "auth=(username, password)" it works fine and returns the json of the record, so I am able to connect to the API, just not put anything into it with Python. Again, basically the same exact thing works in Postman.
What exactly am I doing wrong and how do I put in the data?
According to the requests documentation you're not filling out the put function correctly. Try it like this?
import json
import requests
url = 'https://website.com/rest/site/' + record_id
headers = {
"Content-Type": "application/json",
'Accept':'application/json'
}
payload = json.dumps({
"data_goes_here": None
})
rv = requests.put(url, data=payload, headers=headers, verify=False)
if rv.status_code > 399:
rv.raise_for_status()
print(json.loads(rv.text))

Django rejects Requests' CSRF Token

I'm writing an Ajax post with python's Request's library to a django backend
Code:
import requests
import json
import sys
URL = 'http://localhost:8000/'
client = requests.session()
client.get(URL)
csrftoken = client.cookies['csrftoken']
data = { 'file': "print \"It works!\"", 'fileName' : "JSONtest", 'fileExt':".py",'eDays':'99','eHours':'1', 'eMinutes':'1' }
headers = {'Content-type': 'application/json', "X-CSRFToken":csrftoken}
r = requests.post(URL+"au", data=json.dumps(data), headers=headers)
Django gives me a 403 error stating that the CSRF token isn't set even though the request.META from csrf_failure() shows it is set. Is there something I'm missing or a stupid mistake I'm not catching?
I asked my friend and he figured out the problem, basically you have to send the cookies that django gives you every time you do a request.
corrected:
cookies = dict(client.cookies)
r = requests.post(URL+"au", data=json.dumps(data), headers=headers,cookies=cookies)
You need to pass the referer to the headers, from the django docs:
In addition, for HTTPS requests, strict referer checking is done by
CsrfViewMiddleware. This is necessary to address a Man-In-The-Middle
attack that is possible under HTTPS when using a session independent
nonce, due to the fact that HTTP ‘Set-Cookie’ headers are
(unfortunately) accepted by clients that are talking to a site under
HTTPS. (Referer checking is not done for HTTP requests because the
presence of the Referer header is not reliable enough under HTTP.)
so change this:
headers = {'Content-type': 'application/json', "X-CSRFToken":csrftoken, "Referer": URL}

Accessing Imgur API with Python 3.4.1 and Urllib3

I am trying to wrap my head around the Imgur API. I have found some good examples of how to send the authorization header to Imgur, however they all use urllib2, and I apparently, using pyhton 3.4.1 can only use urllib3.
So I have tried a couple of things and none of them seem to be working.
from this post I tried using the basic_auth header:
http = urllib3.PoolManager()
header = urllib3.make_headers(basic_auth="Client-ID" + CLIENT_ID)
r = http.request('GET', 'https://api.imgur.com/3/gallery/r/pics', headers=header)
that gives me a 403 error.
from this post I tried this method instead:
http = urllib3.PoolManager()
header= {"Content-Type": "text", "Authorization": "Client-ID" + CLIENT_ID}
r = http.request('GET', 'https://api.imgur.com/3/gallery/r/pics', headers=header)
that also returns a 403.
Now however I have got a step closer by reading the urllib3 documents and tried sending the Authorization as a field instead.
http = urllib3.PoolManager()
r = http.request('GET', 'https://api.imgur.com/3/gallery/r/pics', fields={"Authorization": "Client-ID " + CLIENT_ID})
this however returns a 401.
so can some one help me to figure out basic anonymous interaction with the Imgur API using these, or other methods?
Per imgur's API documentation, you have to send the auth header as such:
Authorization: Client-ID YOUR_CLIENT_ID
In this line:
header = urllib3.make_headers(basic_auth="Client-ID" + CLIENT_ID)
you are sending it as:
Authorization: Client-IDYOUR_CLIENT_ID
You need a space between.

bad request error 400 while using python requests.post function

I'm trying to make a simple post request via the requests library of Python and I get a bad request error (400) while my url is supposedly correct since I can use it to perform a get.
I'm very new in REST requests, I read many tutorials and documentation but I guess there are still things I don't get so my error could be basic. Maybe a lack of understanding on the type of url I'm supposed to send via POST. Here my code :
import requests
v_username = "username"
v_password = "password"
v_headers = {'content-type':'application/rdf+xml'}
url = 'https://my.url'
params = {'param': 'val_param'}
payload = {'data': 'my_data'}
r = requests.post(url, params = params, auth=(v_username, v_password), data=payload, headers=v_headers, verify=False)
print r
I used the example of the requests documentation.
I had a similar problem, i tried changing params to data or with json.dumps():
from json import dumps
r = requests.post(url, params=dumps(params), auth=(v_username, v_password), data=payload, headers=v_headers, verify=False)
or
r = requests.post(url, data=dumps(params), auth=(v_username, v_password), data=payload, headers=v_headers, verify=False)

Categories