Authorization in Python SDK
This documentation provides an overview of how to configure and authorize requests to the Aspose Barcode Cloud API using JWT and external authorization methods. It covers key code examples and settings needed to enable secure and authorized API requests.
Note: For manage authorization you should have a Client Id and Client Secret. How to get it described in Quick Start Section
Overview
Aspose Barcode Cloud API supports two main authorization methods:
- JWT (JSON Web Token) - Allows token-based authentication where tokens are obtained using Client Id and Client Secret.
- External Authorization - Permits manual setting of JWT tokens for custom authorization.
JWT Authorization
When using JWT, the API classes automatically obtains a token and includes it in each API request.
Setting Up JWT Authorization
To set up JWT authorization, specify the client_id
and client_secret
:
config = new Configuration
(
client_id = "<Your-Client-Id>",
client_secret = "<Your-Client-Secret>"
)
generateApi = GenerateApi(ApiClient(config))
Upon setup, the internal SDK methods handles token generation and token refresh:
- The RequestToken method requests a token using
client_id
andclient_secret
. - The token is added to the Authorization header for each request.
External Authorization
External authorization allows you to manually manage tokens. Assign your JWT token directly to access_token
and leave client_id
and client_secret
blank.
Setting Up External Authorization
Example configuration with External Authorization:
config = Configuration(
(
access_token = "<Your-External-Jwt-Token>"
)
recognizeApi = RecognizeApi(ApiClient(config))
In this mode, the API sends the provided JWT in the Authorization header without attempting to generate or refresh the token.
How to Fetch a Token without SDK Internal Methods
To fetch a new token, send a POST request to https://id.aspose.cloud/connect/token with the application/x-www-form-urlencoded
content type. In the request body, specify the following parameters:
grant_type=client_credentials
client_id=<Your-Client-Id>
client_secret=<Your-Client-Secret>
A Code example for fetching token:
import requests
def main():
client_id = "Client Id from https://dashboard.aspose.cloud/applications"
client_secret = "Client Secret from https://dashboard.aspose.cloud/applications"
base_url = "https://id.aspose.cloud/"
endpoint = "connect/token"
# Check the client_id is changed to not break github ci pipeline
if client_id.startswith("Client Id"):
print("client_id not configured. Skip this snippet test")
return
payload = {"grant_type": "client_credentials", "client_id": client_id, "client_secret": client_secret}
response = requests.post(f"{base_url}{endpoint}", data=payload)
response.raise_for_status()
data = response.json()
token = data["access_token"]
print("Token recieved successfully")
# Uncomment next line to view token
# print(token)
if __name__ == "__main__":
main()
Conclusion
Configuring authorization in the Aspose.BarCode.Cloud SDK for Python allows secure, authenticated access to barcode-related functionalities. Choose JWT authorization wiht client id and secret for automatic token management or external for custom token handling.
With these examples and explanations, you should be able to set up authorization for the Aspose.BarCode.Cloud SDK for Python.