Python Signing Guide
This section uses IntelliJ IDEA as an example to describe how to integrate the Python SDK for API request signing. You can import the sample project in the code package, and integrate the signing SDK into your application by referring to the API calling example.
Preparing the Environment
- Download IntelliJ IDEA 2018.3.5 or later from the IntelliJ IDEA official website and install it.
- Download the Python installation package (version 2.7.9 or later, or 3.x) from the Python official website and install it.
After Python is installed, run the pip command to install the requests library.
pip install requests
If a certificate error occurs during the installation, download the get-pip.py file to upgrade the pip environment, and try again.
- Install the Python plug-in on IDEA.
Obtaining the SDK
Log in to the APIG console and choose Help Center > SDK Process Flow. Then download the SDK.
The following table shows the directory structure of the downloaded package.
|
Name |
Description |
|---|---|
|
apig_sdk\__init__.py |
SDK code |
|
apig_sdk\signer.py |
|
|
main.py |
Sample code |
|
licenses\license-requests |
Third-party license |
Importing the Sample Project
- Start IDEA and choose File > New > Project.
On the displayed New Project page, choose Python and click Next.

- Click Next. Click ..., select the directory where the SDK is decompressed, and click Finish.

- View the directory structure shown in the following figure.

Request Signing and API Calling
After the calling information is modified, the sample code can be directly called. For details about the calling information, see Preparations.
- Run the pip command to install the requests library.
1pip install requests
- Import apig_sdk to the project.
1 2
from apig_sdk import signer import requests
- Generate a new signer and enter the AK and SK.
- In this example, the AK and SK stored in the environment variables are used. Specify the environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment first. The following uses Linux as an example to describe how to set the obtained AK/SK as environment variables.
- Open the terminal and run the following command to open the environment variable configuration file:
- Set environment variables, save the file, and exit the editor.
export CLOUD_SDK_AK="Obtained AK" export CLOUD_SDK_SK="Obtained SK"
- Run the following command to apply the modification:
- Generate a new signer and enter the configured environment variables.
1 2 3 4
sig = signer.Signer() # Set the AK/SK to sign and authenticate the request. sig.Key = os.getenv('CLOUD_SDK_AK') sig.Secret = os.getenv('CLOUD_SDK_SK')
- In this example, the AK and SK stored in the environment variables are used. Specify the environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment first. The following uses Linux as an example to describe how to set the obtained AK/SK as environment variables.
- Generate a new request, and specify the domain name, method, request URI, and body.
Take the API for querying VPCs as an example: HTTP method GET, endpoint service.region.example.com, URI /v1/77b6a44cba5143ab91d13ab9a8ff44fd/vpcs, and query parameter limit=1.
1 2 3
# The following example shows how to set the request URL and parameters to query a VPC list. r = signer.HttpRequest("GET", "https://service.region.example.com/v1/77b6a44cba5143ab91d13ab9a8ff44fd/vpcs?limit=1") # r.body = "{\"a\":1}"
- Add other headers required for request signing or other purposes. For example, add the X-Project-Id header in multi-project scenarios or the X-Domain-Id header for a global service. Separate multiple headers with commas.
1 2
r.headers = {"X-Project-Id": "xxx"} r.headers = {"X-Domain-Id": "xxx"}
- Execute the following function to add the X-Sdk-Date and Authorization headers for signing:
1sig.Sign(r)
- X-Sdk-Date is a timestamp request header parameter that must be signed.
- The Authorization header is a mandatory request header parameter for security authentication.
- The SDK automatically completes signing requests, and you do not need to know which header parameters are involved in the signing process.
- Access the API and view the access result.
1 2 3
resp = requests.request(r.method, r.scheme + "://" + r.host + r.uri, headers=r.headers, data=r.body) print(resp.status_code, resp.reason) print(resp.content)
The following uses the API for querying VPCs as an example. The access result is as follows:
{ "vpcs": [ { "id": "13551d6b-755d-4757-b956-536f674975c0", "name": "default", "description": "test", "cidr": "172.16.0.0/16", "status": "OK", "enterprise_project_id": "0", "routes": [], "block_service_endpoint_states": "off", "enable_network_address_usage_metrics": false, "tenant_id": "087679f0aa80d32a2f4ec0172f5e902b", "created_at": "2022-12-15T02:11:13", "updated_at": "2022-12-15T02:11:13" } ] }
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.
