Skip to main content

Automates logging into web UIs to access unofficial APIs

Project description

Introduction

We want to use APIs from web UIs that require login. What we want to do is to set up a session, login and then use the API of the page. The reason we want to do this is to use existing APIs. For instance, the user group management service at KTH, see 1{reference-type="ref" reference="UGEditor"}. We can then use the service, track the requests in the browser's developer tools. Then we can simply make the same requests from Python.

Screenshot of the KTH UG Editor with Firefox's Developer Tools open, showing network requests made.

For instance, we can redo the request in Figure 1 (above) like this:

import weblogin
import weblogin.kth
import os

ug = weblogin.AutologinSession([
                weblogin.kth.UGlogin(os.environ["KTH_LOGIN"],
                                     os.environ["KTH_PASSWD"],
                                     "https://app.kth.se/ug-gruppeditor/")
            ])

response = ug.get("https://app.kth.se/ug-gruppeditor/api/ug/users"
                  "?filter=memberOf eq 'u26yk1i3'")

The code above will access the API used by the KTH UG group editor service. It will automatically sign in when needed. The API URLs don't trigger a redirect to log in, they just give a 401 unauthorized error. However, we can use the main URL to the UI to trigger such an event, log in and then access the API. All this happens automatically in the background.

The way we do this is to subclass the requests.Session class to intercept all requests of a session to check for signs indicating that we must log in. When we detect such sign, we log in and resume as if nothing ever happened.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

weblogin-1.16.tar.gz (36.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

weblogin-1.16-py3-none-any.whl (35.3 kB view details)

Uploaded Python 3

File details

Details for the file weblogin-1.16.tar.gz.

File metadata

  • Download URL: weblogin-1.16.tar.gz
  • Upload date:
  • Size: 36.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.7 Linux/6.17.0-14-generic

File hashes

Hashes for weblogin-1.16.tar.gz
Algorithm Hash digest
SHA256 a204359351cf0104e8338bfa93846dbda8ad364a6939d19bfd2d65f55813f530
MD5 6526525a13409481625e959b51cc6e8e
BLAKE2b-256 debd448eb13e8c4e5ac93f3d9f90da691a87cca4674f105290ea998db89e091d

See more details on using hashes here.

File details

Details for the file weblogin-1.16-py3-none-any.whl.

File metadata

  • Download URL: weblogin-1.16-py3-none-any.whl
  • Upload date:
  • Size: 35.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.7 Linux/6.17.0-14-generic

File hashes

Hashes for weblogin-1.16-py3-none-any.whl
Algorithm Hash digest
SHA256 6e5a22b7728e8e128c48b147c6883dd7990400f896b180581844c3a7e4e0a85c
MD5 3b1aad8f441e8efd7225e3fc38d42623
BLAKE2b-256 bd44b628f29dc4ca0a05714a61d4ba7d518c383d707a2d2c4e0d3c8eb7329470

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page