API get request not returning correct data in python - python-3.x

I am trying to get data from Uni Stats API by sending a get request. Following is my code:
import requests
dt = requests.get(url='https://API_TOKEN#data.unistats.ac.uk/api/v4/KIS/Institution/10007856.json')
print(dt)
Whenever I run this code it returns me <401 Response> but If I try the same url https://API_TOKEN#data.unistats.ac.uk/api/v4/KIS/Institution/10007856.json in the Postman software it returns correct data in json format.
So I wanted to know how can I send a get request with requests which can return correct data?

requests.get('https://data.unistats.ac.uk/api/v4/KIS/Institution/10007856.json', auth=('user', 'pass'))
401 is an authorization error, provide authorisation to the request.get method.
Depending on your user agent, you may be able to include the access token in the url as in the following examples:
https://accesstoken#data.unistats.ac.uk/api/v4/KIS/Institution/{ukprn}/Course/{kisCourseId}
Emphasis added.

Related

How to use jwt authorization with python's library requests?

I'm developing a Flask RESTFULL API with flask_jwt_extended library as extension for authorization.
Now, I have two usuals resources for register an user and for login, that works perfectly. With login resource an user can give their email and password and get back a token. From Postman this token works as expected using Bearer Token mode.
However, I need to make some requests directly from Python3 terminal. Until now, I was using a simple name-password authentication in my requests, so an example of a request would be:
import requests as rs
rs.get('http://localhost:5000/api/samples', auth = ('user', 'pass'))
Now, using jwt tokens, I'm trying the following:
import requests as rs
# xxxxxx is a really long string got previously with login resource
rs.get('http://localhost:5000/api/samples', auth = ("xxxxxx"))
but this approach give to me:
TypeError: 'str' object is not callable
In other posts I've checked that this type of authorization is made through headers, so I've also tried:
import requests as rs
# xxxxxx is a really long string got previously with login resource
rs.get('http://localhost:5000/api/samples', headers = {'Authorization': 'access_token xxxxxx'})
and the error here is an instantly 401 http response.
Hope someone could help me!
Thanks you in advance!
If someone comes here looking for an answer: you just need to add a header just like the following
headers = {'Authorization': 'Bearer {token}'}
Source: https://reqbin.com/req/python/5k564bhv/get-request-bearer-token-authorization-header-example

Twitter Search API: Getting error 32 could not authenticate you with specific searches but not every search

I'm using python3 to send a request to the Twitter Search API. I'm testing these requests in Postman, and then using the code snippit generator to copy and paste into my editor to run like so:
url = "https://api.twitter.com/1.1/search/tweets.json?q=twitter"
payload={}
headers = {
'Authorization': 'OAuth oauth_consumer_key="",oauth_token="",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1613517432",oauth_nonce="x",oauth_version="1.0",oauth_body_hash="",oauth_signature=""',
'Cookie': 'personalization_id=""; lang=en; guest_id='
}
twitter_response = requests.request("GET", url, headers=headers, data=payload)
The requests work just fine in Postman. But in my script, the request only works when the query parameter for the URL is "nasa". If I change it to anything else, I get error 32 Could not authenticate.
Is there some quirk about Python I don't know about when it comes to the Twitter Search API? Any guidance would be helpful here.
Sounds like you’re copying and pasting the OAuth headers from the Twitter Dev docs instead of calculating them with every new query. I think the docs use NASA as an example search query.
I was able to find a way around this by using the Tweepy API wrapper, which solved my OAuth issues. The headers weren't getting formatted properly for the request.

How to get data from Response with Python Selenium in DevTools

Maybe someone knows how to get data from Response in DevTools with Selenium?
https://i.stack.imgur.com/T9ALa.png
I don`t know how to get response data.
It's as easy as this:
response = driver.execute_script(javascript)
Now response has what was returned by the script!
What you are asking for is the HTTP response of a specific request happening in the website. Getting performance info is possible by executing javascript but you won't be able to get the response of all the requests which is happening in the page, atleast from what I know.
However, if you already know the exact request (or you can get it by this way), you can get the response easily by simulating that request in python. Here is an example -
import requests
headers = {
"your_header_keys" : "your_header_values"
}
data = [
"your_request_body"
]
response = requests.post('your_url', headers=headers, data=data)

Use result of post in a subsequent post request

I'm a newbie and have little knowledge, I apologise in advance.
I have been trying to find examples on making subsequent post requests using data from the first request.
Scenario, making a request to obtain an auth token which will then be used on subsequent requests in the header.
E.g. request 1 is a POST and returns an access_token object
request 2 is a POST and requires the access_token object from request 1 in the headers.
I have successfully made the request 1 with request-promise but don't know how to pass that to the 2nd requests options.
Thanks
Look into "promise chaining". You should make the second request in the .then((resp) => ...) callback, where you have access to the response data from the first request. If you return the second request promise, you can continue the chain.

How to send a http response using koajs

I'm trying to validate a webhook via facebook. So facebook hits my url my-url/facebook/receive within my route in nodejs i'd do res.send(req.query['hub.challenge']); to send an http response.
I'm using KoaJS. From what i understand, Koajs merges the request and response object into ctx but when reading through the docs I can't find anything along the lines of ctx.send or similar to send a http response.
Can anyone give me some direction or links.
Thanks.
To send the body of a response, you can simply do ctx.response.body = 'Hello'. There are many aliases attached to ctx, so you don't necessarily have to reference the response or request yourself. Doing ctx.body = 'Hello' would be the same as the code above.
If you wanted to set headers, you would use the ctx.set() method. For example: ctx.set('Content-Type', 'text/plain').
To access the query parameters, you would use ctx.request.query['some-key'] (or simply the alias ctx.query['some-key']).
All of the different request/response methods are documented pretty well at the Koa website along with a list of aliases attached to ctx. I highly recommend you give it a read.

Resources