Skip to main content

Automates logging into web UIs to access unofficial APIs

Project description

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.{#UGEditor width="\columnwidth"}

For instance, we can redo the request in 1{reference-type="ref" reference="UGEditor"} like this:

from weblogin.kth import AutologinSession
import os

ug = AutologinSession(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-0.4.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

weblogin-0.4-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: weblogin-0.4.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.6 Linux/5.15.0-52-generic

File hashes

Hashes for weblogin-0.4.tar.gz
Algorithm Hash digest
SHA256 b500b383cdee0c07af776276a564376a19fd3c653ba7d9289582d14e9edf34a7
MD5 b423fd9fd2ed2895d4165dc4722469e1
BLAKE2b-256 72a44ebc2568ba1d3aadd64ee58919dbf95b1165fa5d7a96cdfedf7534d3b7fc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: weblogin-0.4-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.6 Linux/5.15.0-52-generic

File hashes

Hashes for weblogin-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 20f39ad90466216325f4251fb8e48bb869a7618a9211a677f66ea177023814e4
MD5 4d6d2e416b16a4b736a8da41615d69b0
BLAKE2b-256 02875eb542b58bebf87e9f491a08f2230b2a48bf7cf90038828a3e4235a29675

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