Generate the JWT (JSON web token) for Shaarli API authentication using Python and Javascript

Official documentation of Shaarli only gives a PHP example, but I’m more of a Python kind of guy.

This is how to generate a JSON web token (JWT) using some Python or Javascript code.

Python

Keep in ming this uses PyJWT (pip install PyJWT) and not jwt.

Replace SHAARLI_API_SECRET accordingly:

#!/usr/bin/python3
import time
import jwt
now = int(time.time())
payload = { "iat" : now }
SHAARLI_API_SECRET = "your shaarli REST API Secret"
SHAARLI_HASH_ALGORITHM = "HS512"
token = jwt.encode(payload, SHAARLI_API_SECRET, algorithm = SHAARLI_HASH_ALGORITHM)

print(token)

Javascript

Here’s an example with javascript (npm install jsonwebtoken)

let jwt = require('jsonwebtoken');
let now = Math.floor(Date.now() / 1000)

const shaarli_api_secret = 'your shaarli REST API Secret"';
const payload = {
  "iat": now
};

let token = jwt.sign(payload, shaarli_api_secret, { algorithm: 'HS512'});

console.log(token)

Test your JWT

You can now use the JWT (replace <your-newly-obtained-jwt-token> with the output of previous script):

curl --request GET \
  --url 'https://bookmark.example.com/api/v1/links?searchterm=linux&limit=1' \
  --header 'Authorization: Bearer <your-newly-obtained-jwt>'

Output

[{"id":4947,"url":"https:\/\/wiki.archlinux.org\/title\/NetworkManager#Configuring_MAC_address_randomization","shorturl":"4X0sQg","title":"NetworkManager - ArchWiki","description":"configure MAC address randomization using NetworkManager","tags":["linux","mac"],"private":false,"created":"2022-12-16T18:10:33+01:00","updated":"2022-12-18T23:00:07+01:00"}]



Thanks for reading this post!


Did you find an issue in this article?

- click on the following Github link
- log into Github with your account
- click on the line number containing the error
- click on the "..." button
- choose "Reference in new issue"
- add a title and your comment
- click "Submit new issue"

Your feedback is much appreciated! πŸ€œπŸΌπŸ€›πŸΌ

You can also drop me a line below!