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.
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.
Metadata
Release files for weblogin 1.24
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| weblogin-1.24.tar.gz | 103.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| weblogin-1.24-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 162.7 kB
Release files / weblogin-1.24.tar.gz
| Download URL | weblogin-1.24.tar.gz |
|---|---|
| Size | 103.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c1060517248b8f1a1a375c1b332cfb4f12003d3af51b8e193abb253a73611c19
|
|
BLAKE2b-256 checksum How to use checksums |
2a13bec8eeaa37c82825ac1ce1f1cee3f201906afd145450860181c52bde8816
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.3 CPython/3.14.4 Linux/7.0.0-31-generic
|
Release files / weblogin-1.24-py3-none-any.whl
| Download URL | weblogin-1.24-py3-none-any.whl |
|---|---|
| Size | 59.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
15f8e494c705037d905ee2fbce951ff07a5437f973ce294bd783700b0687410b
|
|
BLAKE2b-256 checksum How to use checksums |
f34fc06d69f42befc0265fb3a9daf82329fe933e684b7a67e0069db41fd2b112
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.3 CPython/3.14.4 Linux/7.0.0-31-generic
|