ورود کاربر
در این مثال میبینید که چگونه با SDK زربان احراز هویت کاربر را پیاده میکنید: کاربر را وارد میکنید، توکن احراز هویت را نگه میدارید و برای این فرایند لاگگیری درستی راه میاندازید.
پیشنیازها
پیش از اجرای این مثال، مطمئن شوید که:
-
SDK زربان را نصب کردهاید:
pip install zarban -
به API زربان در محیط تست دسترسی دارید
مشخصات API
نقطه پایانی: /auth/login
- متد: POST
- توضیحات: ورود با رایانامه و گذرواژه و دریافت توکن JWT.
فرمت درخواست
{
"email": "example@domain.com",
"password": "password"
}
فیلدهای ضروری
| فیلد | نوع | توضیحات | مثال |
|---|---|---|---|
| string | ایمیل کاربر | example@domain.com | |
| password | string | رمز عبور کاربر | password |
فرمت پاسخ
پاسخ موفق (200 OK)
{
"token": "eyJhbGciOiJIUzI1NiIsInR..."
}
پاسخهای خطا
400Bad Request
{
"msg": "Bad request",
"reasons": ["Invalid address"]
}
401Unauthorized
{
"msg": "Unauthorized",
"reasons": ["Invalid credentials"]
}
404Not Found
{
"msg": "Not Found",
"reasons": ["User not found"]
}
500Internal Server Error
{
"msg": "Internal Server Error",
"reasons": ["Server error occurred"]
}
نمونه کد
import zarban.wallet.openapi_client as wallet
import logging
def login_example():
# Set up logging
logger = logging.getLogger(__name__)
# Create and configure the Configuration object
cfg = wallet.Configuration(
host="https://testwapi.zarban.io" # Use the working API URL
)
# Create an instance of the ApiClient with the configuration
api_client = wallet.ApiClient(cfg)
# Create an instance of the DefaultApi using the ApiClient
auth_api = wallet.AuthApi(api_client)
# Prepare the login request data
login_request = wallet.LoginRequest(
email="user@example.com",
password="your_secured_password"
)
try:
# Call the login API
api_response = auth_api.login_with_email_and_password(login_request)
logger.info("Login successful!")
logger.info(f"Token: {api_response.token}")
# After successful login, you can set the access token for future authenticated requests
cfg.access_token = api_response.token
return api_response.token
except wallet.ApiException as e:
logger.error(f"Exception when calling auth_api->login_with_email_and_password: {e}")
logger.error(f"Status code: {e.status}")
logger.error(f"Reason: {e.reason}")
logger.error(f"Error message: {e.body}")
return None
if __name__ == "__main__":
token = login_example()
if token:
print(f"Login successful. Token: {token}")
else:
print("Login failed. Please check the logs for more information.")