You can generate a token from the web app and then use it in the API.
I succeeded this way.
I want to access traccar from another application (customer portal), and therefore automatically generate tokens for my users
Then I recommend checking how OIDC works in the official web app.
This is not possible with the API with an administrator account?
Should be possible.
i try something like this but don't work/api/session/token?userId=3&token=TOKENADMIN
with expiration in body
Make sure you provide a link to the documentation or source code where you found this.
Ok that can't work
@Path("token")
@POST
public String requestToken(
@FormParam("expiration") Date expiration) throws StorageException, GeneralSecurityException, IOException {
Date currentExpiration = (Date) request.getSession().getAttribute(EXPIRATION_KEY);
if (currentExpiration != null && currentExpiration.before(expiration)) {
expiration = currentExpiration;
}
return tokenManager.generateToken(getUserId(), expiration);
}
Yes, it can't work. Can you still provide a link to where you found it, so we can fix it.
On a post in the forum, which talked about the API that I remixed.
After some testing, I managed to develop this code and it works as I want.
However, I don't know if this is the best solution.
import requests
# Base URL
base_url = "https://example.com"
# Create a session object
s = requests.Session()
# Function to send a POST request
def send_post_request(endpoint, data, headers=None):
url = f"{base_url}/{endpoint}"
response = s.post(url, data=data, headers=headers)
return response.json()
# Function to send a GET request
def send_get_request(endpoint, token=None):
url = f"{base_url}/{endpoint}"
if token:
headers = {'Authorization': 'Bearer ' + token}
response = s.get(url, headers=headers)
else:
response = s.get(url)
print("Status code:", response.status_code)
print("Response text:", response.text)
if response.text: # Check if the response is not empty
return response.json()
else:
return None # or return an appropriate value
def send_post_request_raw(endpoint, data, headers=None):
url = f"{base_url}/{endpoint}"
response = s.post(url, data=data, headers=headers)
return response.text
# Your token
token = "Admin token"
# Retrieve the session of the specific user
user_id = 2 # ID of the user you want to retrieve
user_session = send_get_request(f"api/session/{user_id}", token)
print("User session:", user_session)
user_session = send_get_request(f"api/session")
print("User session:", user_session)
# Create a new session with a specific token
data = {'expiration': '2024-05-16T22:00:00.000Z'}
new_token_raw = send_post_request_raw(f'api/session/token', data)
print("New token (raw):", new_token_raw)
s.cookies.clear()
s = requests.Session()
user_session = send_get_request(f"api/session?token={new_token_raw}")
print("User session:", user_session)
s.cookies.clear()
Hello,
I'm looking to call the API by users who are registered using the OIDC login.
Thanks you