how to update bearer token or obtain bearer token POSTMAN - python

i run my collection postman using python request.
my problem is , the beare token is not live lifetime, if bearer token expired , the status_code will 401
here my code python to run my postman collection
`
import request
headers = {
'Connection':'keep-alive',
'Content-Length':'0',
'pm-u':'https://cat-fact.herokuapp.com/facts/',
'sec-ch-ua':'"Chromium";v="111", "Not(A:Brand";v="8"',
'pm-h0':'Accept=application/json, Content-Type=application/x-www-form-urlencoded, User-Agent=PostmanRuntime/7.30.1, Host=cat-fact.herokuapp.com, Accept-Encoding=gzip%2C deflate%2C br, Connection=keep-alive, Cookie=connect.sid%3Ds%253AcuHspVkAp4tLywV_aFygdKEhUHJWKFjk.Ngw7TkSnS%252FXgBT8fy7V8fHDdaXm7247iMObs0dO3gXU',
'sec-ch-ua-mobile':'?0',
'Authorization':'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY0NDQ5MTM2LWZmZTgtNGVlNi05ODhlLWFkZTg1NWM4YzZjZCIsInVzZXJJZCI6MjU2ODQzODUsInRlYW1JZCI6NDY0OTI3MSwiaXYiOiJVSERBbnhVWVlsNkluQ1hyOFRkQTdnPT0iLCJhbGdvIjoiYWVzLTEyOCIsImlhdCI6MTY3NTY4NTY5MywiZXhwIjoxNjc1Njg3NDkzfQ.cKI8rbIYxZlNUAeyi88Jz8d0GnxjPY-AwI0KRLuIBw0',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
'pm-o0':'method=GET, timings=true, timeout=180000, rejectUnauthorized=false',
'Content-Type':'application/x-www-form-urlencoded',
'sec-ch-ua-platform':'"Windows"',
'Accept':'*/*',
'Origin':'https://warped-moon-744330.postman.co',
'Sec-Fetch-Site':'same-site',
'Sec-Fetch-Mode':'cors',
'Sec-Fetch-Dest':'empty',
'Referer':'https://warped-moon-744330.postman.co/workspace/My-Workspace~ee34daa5-902b-48f2-871d-733782fc8e61/request/25684385-fb05a398-45f8-45c1-9a9a-3c120d3d7e23',
'Accept-Encoding':'gzip, deflate, br',
'Accept-Language':'en-US,en;q=0.9'}
payload=None
response0 = requests.request("POST", "https://orion-http.gw.postman.co/v1/request", headers=headers, data=payload)
print(response0.status_code)`
i want to generate 'Authorization': bearer token from postman , but how ?

Related

python-requests CSRF verification failed

I'm trying to use a python script to access on a website
But it tells me CSRF verification failed. Request aborted. The python-requests code is as followed:
import requests
payload = {
'inUserName': 'NAN',
'inUserPass': 'NAN'
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/103.0.0.0 Safari/537.36',
'From': 'https://pruebita.gob.pe/accounts/login/',
}
with requests.Session() as s:
p = s.post('https://pruebita.gob.pe/accounts/login/', data=payload, headers=headers)
print (p.text)
r = s.get('A protected web page url')
print (r.text)

How to bypass 401 error for this api with python

I download options chain data from this page: https://opstra.definedge.com/optionchain
I found an api to download the json data with python here at https://opstra.definedge.com/api/openinterest/optionchain/free/NIFTY&26MAY2022
All was working fine for few months. Suddenly I started getting a 401 error. How can I bypass the 401 error?
I have a account to authenticate myself against the server.
My previous python code that is not working anymore
import requests
sym = "NIFTY"
exp_date = '26MAY2022'
# def oc(sym,exp_date ):
url = "https://opstra.definedge.com/api/openinterest/optionchain/"+sym+'&'+exp_date
headers = { "accept-encoding" : "gzip, deflate, br",
"accept-language" : "en-US,en;q=0.9",
"referer": "https://opstra.definedge.com/optionchain",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.136 Safari/537.36"}
response = requests.get(url, headers=headers)
print(response)
Can anyone help me in getting the data again with python?

JWT Bearer Authorization for web scraping using python requests

This is my first post on StackOverflow so please bear with me.
I am writing a function that makes a request via REST API and then returns the values, but I'm having trouble with the authentication part.
The authentication is a JWT bearer token, and is needed to retrieve the data (though I am not needing to log in so in that regard it is an unauthorised API).
def get__price(jwt, cookie):
headers = {
'authority': 'www.dextools.io',
'pragma': 'no-cache',
'cache-control': 'no-cache',
'accept': 'application/json',
'authorization': f'Bearer {jwt}', # HERE IS THE VAR I NEED
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
'content-type': 'application/json',
'sec-gpc': '1',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
'referer': 'https://www.dextools.io/app/uniswap/pair-explorer/0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852',
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
#'cookie': f'__cfduid={cookie}; ai_user=hizb^|2021-04-03T00:16:45.460Z; ai_session=5vAmv^|1617443356577.045^|1617443356577.045',
}
params = (
('v', '1.9.1'),
('pair', '0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852'),
('ts', '1617443384-0')
)
try:
response = requests.get('https://www.dextools.io/api/uniswap/1/pairexplorer', headers=headers, params=params)
except Exception as e:
print(f"ERROR: {e}")
I've tried to make a request to the website https://www.dextools.io and get any JWT tokens, but it doesnt seem to work using Sessions.
Maybe it has no importance but I can find this JWT token on the browser when I go to developer tools > Local Storage > (website url) > t where t contains my eyJxxxxxxxxxxxxxxx token.
Any help would be appreciated, thanks.
Hello seeing to the network requests of website I was able to get the data via below code but you might need to get the new password if website blocks it jwt token which is generated below is valid for like 6 to 8 mins you can re use the jwt token till that time and then you need to get new jwt token by calling that back login url like mentioned in below code.
Code:
import time
import requests
s = requests.session()
headersdict = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
'Referer': 'https://www.dextools.io/app/uniswap/pair-explorer/0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852',
'Origin': 'https://www.dextools.io'}
s.headers.update(headersdict)
payload = {"id": "anyone", "password": "TfY6WC6F4L4+S6xwvPo8QoHlYZ50rK2DrJnEAWBoMqU="}#you can use this password to generate new jwt tokens if it blocks you check network requests and get this password again but i dont think they will block it that way.
s1 = s.post("https://www.dextools.io/back/user/login", json=payload)
jwt = s1.headers["X-Auth"]
headersdict = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
'Referer': 'https://www.dextools.io/app/uniswap/pair-explorer/0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852',
'Origin': 'https://www.dextools.io',
'authorization': f'Bearer {jwt}'}
s.headers.update(headersdict)
params = (
('v', '1.9.1'),
('pair', '0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852'),
('ts', f'{time.time()}-0')
)
response = s.get('https://www.dextools.io/api/uniswap/1/pairexplorer', params=params)
print(response.text)
Output:
Let me know if you have any questions :)

error in $ failed reading not a valid json value when i try to send requests via python

so ive been trying to figure out how to do the 'follow' thing using python codes on imvu.com, but it always returns the message "invalid arguments" error in $: failed reading: not a valid json value"
import requests
headers = {
"Origin": "https://secure.imvu.com/",
"Referer": "https://secure.imvu.com/next/av/Sammy165/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36",
"X-IMVU-SAUCE": "" #removed sauce for account safety
}
url = "https://api.imvu.com/profile/profile-user-696969696/subscriptions"
data = {"id": "https://api.imvu.com/profile/profile-user-175389029"}
req = requests.post(url=url, headers=headers, data=data)
print(req.text)
Have you tried
requests.post(url=url, headers=headers, json=data)
?
You have to do json.dumps(data). See code below
import requests
import json
headers = {
"Origin": "https://secure.imvu.com/",
"Referer": "https://secure.imvu.com/next/av/Sammy165/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36",
"X-IMVU-SAUCE": "" #removed sauce for account safety
}
url = "https://api.imvu.com/profile/profile-user-696969696/subscriptions"
data = {"id": "https://api.imvu.com/profile/profile-user-175389029"}
req = requests.post(url=url, headers=headers, data=json.dumps(data))
print(req.text)
Output:
{"status":"failure","error":"ERROR-GENERIC-001","message":"Permission Denied: You are not allowed to modify this subscription set."}

python requests does not POST after redirect

For some reason python requests does not do rePOST after encountered redirect header
import requests
proxies = {'http': 'http://127.0.0.1:8888',}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36',
'content-type': 'application/x-www-form-urlencoded'}
r = requests.post(url, data, headers=headers, timeout=timeout, proxies=proxies, allow_redirects=True,)
html = r.text
So it means I can't login to any form that is behind redirect. How can I solve this issue? Thank you!

Categories