Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authentication and python Requests

I am trying to download some documents using requests, but the page is redirecting me to a userlog in screen and therefor downloading the HTML page.

I've tried doing:

c=requests.get(url,auth=HTTPBasicAuth('user','pass'))

But I'm not getting authenticated.

I've also tried vanilla and Digest.

The form itself looks like this:

<input id="username" name="username" class="required" tabindex="1" type="text" value="" size="25" autocomplete="false"/>
<br/>

<label for="password">Password</label>
<input id="password" name="password" class="required" tabindex="2" type="password" value="" size="25" autocomplete="off"/>

Do I need to pass in the username and password as a part of the payload? If so, how do I do that? I've tried a few different ways so far.

like image 733
James R Avatar asked Sep 12 '12 23:09

James R


People also ask

How do I authenticate API requests in Python?

There are a few common authentication methods for REST APIs that can be handled with Python Requests. The simplest way is to pass your username and password to the appropriate endpoint as HTTP Basic Auth; this is equivalent to typing your username and password into a website.

What is authentication in Python?

Practical Data Science using Python Authentication is the process of determining if the request has come from a valid user who has the required privileges to use the system.

How do you pass authorization Bearer Token in Python requests?

To send a POST JSON request with a Bearer Token authorization header, you need to make an HTTP POST request, provide your Bearer Token with an Authorization: Bearer {token} HTTP header and give the JSON data in the body of the POST message.


1 Answers

Basically, it had to do with grabbing the authentication ID off the page and passing in cookies.

This is basically what I did:

from bs4 import BeautifulSoup as bs
import requests
s = requests.session()
url = r'url_i_care_about'

def authenticate(s, url):
    headers = {'username': 'myuser', 'password': 'mypasss', '_Id': 'submit'}
    page=s.get(url)
    soup=bs(page.content)
    value=soup.form.find_all('input')[2]['value']
    headers.update({'value_name':value})
    auth = s.post(url, params=headers, cookies=page.cookies)
like image 79
James R Avatar answered Sep 23 '22 13:09

James R