Skip to main content

Save this example as intelligence_authenticate.py. Use Python 3 with the requests package installed, set INTELLIGENCE_USER and INTELLIGENCE_PASSWORD in the environment, and replace CUSTOMER_NAME with your instance name. This historical example only checks for HTTP 200 after redirects; that alone does not prove that login succeeded. Verify your instance's authentication behavior before reusing it.

Python 3
import os
from contextlib import contextmanager
import requests

CUSTOMER_NAME = "teste"
INSTANCE_URL = f"https://app-intelligence-{CUSTOMER_NAME}.dadosfera.ai"
INTELLIGENCE_USER = os.environ["INTELLIGENCE_USER"]
INTELLIGENCE_PASSWORD = os.environ["INTELLIGENCE_PASSWORD"]


@contextmanager
def authenticated_session(*args, **kwargs):
#Cria uma sessão autenticada, persistindo o cookie de login
session = requests.Session(*args, **kwargs)
data = {
"username": INTELLIGENCE_USER,
"password": INTELLIGENCE_PASSWORD,
}
resp = session.post(
f"{INSTANCE_URL}/login", timeout=4, data=data, allow_redirects=True
)
if resp.status_code != 200:
raise RuntimeError(
"Failed to create authenticated session: Instance login failed."
)

try:
yield session
finally:
session.close()

Get credentials

Make sure the credentials are set as environment variables.

Authentication function

This function creates an authenticated session that other requests can reuse.