What Headers and Data to Send With My Post Request - python

Very beginner to web. I'm trying to login my router's configuration page which resides at 192.168.1.1 address. There is an authorization process and I inspected the post request being sent when logged in, via dev-tools of the browser. The "Form Data" includes these arguments:
Username: admin
Password: pass
action: login
Response code is 301 (Moved Temporarily) however, when I try to send a post request with python, the response code is 200 and the returned html prints the login layout. Also there are no cookies returned back except this "_TESTCOOKIESUPPORT=1". My code below:
import requests
data = {
"Username": 'admin',
"Password": 'pass',
"action": 'login'
}
session = requests.session()
response = session.post("http://192.168.1.1", data=data)
print(response)
print(response.cookies)
Also when I run my code and refresh the login page from browser I get "wrong credentials" error from the page. What's wrong with my post request?

Related

How to get redirect URL (for Linkedin API)

I need help with the Linkedin API and the requests module please, if anyone knows.
I would like to connect to the linkedin API, I must accept the authorization. Then, it redirects me to a site (which I myself entered as a redirect). And I have to enter the redirect link.
However, I would like to offer this app in production. I would like to be able to retrieve the redirect link, directly with a python code, without having to do it manually.
Anyone know how to do it?
def authorize(api_url,client_id,client_secret,redirect_uri):
# Request authentication URL
csrf_token = create_CSRF_token()
params = {
'response_type': 'code',
'client_id': client_id,
'redirect_uri': redirect_uri,
'state': csrf_token,
'scope': 'r_liteprofile,r_emailaddress,w_member_social'
}
response = requests.get(f'{api_url}/authorization',params=params)
print(f'''
The Browser will open to ask you to authorize the credentials.\n
Since we have not setted up a server, you will get the error:\n
This site can’t be reached. localhost refused to connect.\n
This is normal.\n
You need to copy the URL where you are being redirected to.\n
''')
open_url(response.url)
# Get the authorization verifier code from the callback url
redirect_response = input('Paste the full redirect URL here:')
auth_code = parse_redirect_uri(redirect_response)
return auth_code
```
I am trying to retrieve the redirect_response manually. When I do response.url after connecting to Linkedin it always gives me the connection url

Remote login to decoupled website with python and requests

I am trying to login to a website www.seek.com.au. I am trying to test the possibility to remote login using Python request module. The site is Front end is designed using React and hence I don't see any form action component in www.seek.com.au/sign-in
When I run the below code, I see the response code as 200 indicating success, but I doubt if it's actually successful. The main concern is which URL to use in case if there is no action element in the login submit form.
import requests
payload = {'email': <username>, 'password': <password>}
url = 'https://www.seek.com.au'
with requests.Session() as s:
response_op = s.post(url, data=payload)
# print the response status code
print(response_op.status_code)
print(response_op.text)
When i examine the output data (response_op.text), i see word 'Sign in' and 'Register' in output which indicate the login failed. If its successful, the users first name will be shown in the place. What am I doing wrong here ?
P.S: I am not trying to scrape data from this website but I am trying to login to a similar website.
Try this code:
import requests
payload={"email": "test#test.com", "password": "passwordtest", "rememberMe": True}
url = "https://www.seek.com.au:443/userapi/login"
with requests.Session() as s:
response_op = s.post(url, json=payload)
# print the response status code
print(response_op.status_code)
print(response_op.text)
You are sending the request to the wrong url.
Hope this helps

Python Sessions/Requests Losing Authentication Mid-Session

I'm trying to login to Campaign Monitor to scrape some data from pages related to email campaign performance.
The "login-protected" URL of the page I'm trying to access looks like this:
https://mycompany.createsend.com/campaigns/reports/lists/DFGDF987GD98F7GD?s=BCV98B5XF54BVC54BC
Going to that page in a web browser (try it here) will redirect to the login page, itself with a URL like this:
https://login.createsend.com/l/98SDF76DS87F68S/DFGDF987GD98F7GD?ReturnUrl=%2Fcampaigns%2Freports%2Flists%2FBCV98B5XF54BVC54BC%3Fs%3BCV98B5XF54BVC54BC&s=7DS6F87S6DF876SDF76
What I've gathered from trying to solve this is that I need to open a session, authenticate on the redirect URL, then request the URL that I actually want (using the authenticated session).
Here is the code I'm using to try to accomplish that:
payload = {
'username': 'myUsername',
'password': 'myPassword',
}
redURL = 'https://login.createsend.com/l/98SDF76DS87F68S/DFGDF987GD98F7GD?ReturnUrl=%2Fcampaigns%2Freports%2Flists%2FBCV98B5XF54BVC54BC%3Fs%3BCV98B5XF54BVC54BC&s=7DS6F87S6DF876SDF76'
with requests.Session() as s:
p = s.post(redURL, data=payload)
# This prints the "success" message I've pasted below
print p.content
r = s.get('https://mycompany.createsend.com/campaigns/reports/lists/DFGDF987GD98F7GD?s=BCV98B5XF54BVC54BC')
# This prints the HTML of the login page again, as if I'm not authenticated
print r.content
Here is the "successful" response after the first POST for the session:
{"MultipleAccounts":false,"LoginStatus":"Success","SiteAddress":"https://mycompany.createsend.com","ErrorMessage":"","SessionExpired":false,"Url":"https://mycompany.createsend.com/login?Origin=Marketing\u0026ReturnUrl=%2fcampaigns%2freports%2flists%2f92D2FBCV98B5XF54BVC%3fs%7DS6F87S6DF876SDF76\u0026s=2FBCV98B5XF54BVC","DomainSwitchAddress":"https://mycompany.createsend.com","DomainSwitchAddressQueryString":null,"NeedsDomainSwitch":false}
Can someone please help me out with why the second request in the session prints the HTML of the login page instead of the HTML of the authenticated version of the page (ie. the page with the data I'm looking for)?

Apidoc for Flask-JWT's authentication url

I have started using apidoc for generating REST api documentation. I have tried different things with it.
In my flask based api application, I use JWT (flask-jwt in particular) for authentication. The doc gets generated properly, but the request from the doc doesn't happen properly.
This is where I have put the docsting for generating the doc
def authenticate(username, password):
"""
#api {post} /auth Authentication url
#apiName Authentication
#apiGroup Login
#apiParam {String} username Username of the user
#apiParam {String} password Password of the user
#apiSuccess {String} access_code JWT
#apiSuccessExample Success Response
HTTP/1.1 200 OK
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGl0eSI6IjM5MDA4MGExLWY0ZjctMTFlNS04NTRkLTI4ZDI0NDQyZDNlNyIsImlhdCI6MTQ1OTE3ODE0NSwibmJmIjoxNDU5MTc4MTQ1LCJleHAiOjE0NTkxNzg0NDV9.nx_1a4RmvJ7Vlf1CvnMzqoTfzChcuJnDb1Tjy1_FnXw"
}
#apiErrorExample Invalid Credentials
{
"description": "Invalid credentials",
"error": "Bad Request",
"status_code": 401
}
"""
user = session.query(User).filter_by(username=username).first()
if user and user.is_correct_password(password):
return user
return False
And when I generate the doc, it get's generated properly
$ apidoc -i onehop/ -o doc
But when I pass credentials from the generated doc, the request fails. The request data I get in flask's request is '' (empty)
It also fails even after including the Content-Type header in the docstring. The request data I get in flask's request is username=test#example.com&password=test123
I have confirmed above by putting debug points in Flask-JWT's code
def _default_auth_request_handler():
data = request.get_json() # My debug poing
username = data.get(current_app.config.get('JWT_AUTH_USERNAME_KEY'), None)
password = data.get(current_app.config.get('JWT_AUTH_PASSWORD_KEY'), None)
But when I make the same request using postman it happens properly.
Postman headers
Apidoc headers
What did I miss?

save session/cookies with requests

I want to send a post request to a login form with the action /ShowLogin.xpl. I wrote a code using requests which sends an http request and sends post request with the user name & password credentials. How over, when I try to print out the content of the web page I still see the login page because the session or the cookie isn't saved. Can anyone help me?
The url is: https://www.xplace.com/ShowLogin.xpl
My code is:
payload = {"j_username" : "myusername", "j_password" : "mypassword"}
with requests.Session() as s:
webpage = s.post("https://www.xplace.com/ShowLogin.xpl", data=payload)
# An authorised request
r = s.get("https://www.xplace.com/ShowRecommendedProjects.xpl")
print r.text

Categories

Resources