This topic describes how to use the Alibaba Cloud Python SDK. It uses GetPhoneNumberIdentificationUrl operation as an example (obtain the verification URL for your phone number).
View the API documentation
Before you call an API operation, we recommend that you read the documentation about the GetPhoneNumberIdentificationUrl operation to learn about the parameters and permissions that are required to call the API operation. For more information, see List of operations by function.
Authentication
Step 1: Create a RAM user and grant permissions
Because your root account has high privileges, we recommend using a RAM user for API calls and routine O&M. For more information, see Overview of RAM users.
Create a RAM user: Go to the Create User page. Specify the required information, select Permanent AccessKey for Access Configuration, then click OK. Save your AccessKey for later use.
Grant permissions to the RAM User: Go to the Users page. Find the RAM user that you created and click Attach Policy in the Actions column. In the Policy search box, enter AliyunDysmsFullAccess, select the policy, and then click OK.
AliyunDytnsReadOnlyAccess: Grants read-only permissions on Cell Phone Number Service.
AliyunDytnsFullAccess: Grants full management permissions on Cell Phone Number Service.
If you need to create a custom policy, see RAM authorization.
Step 2: Obtain access credentials
Configure your AccessKey pair using environment variables. For information about how to configure environment variables, see Configure environment variables on Linux, macOS, and Windows.
To prevent security risks, avoid hard-coding your AccessKey pair. Instead, retrieve it from environment variables.
Call the API operation
In this example, Python SDK is used to call the API. You can use SDKs for other programming languages in a similar way. For more information, see Cell Phone Number Service SDK. You can use other methods to call operations as needed. For more information, see Call methods.
Prepare Python environment
Download and install Python 3. If Python is already installed, run the python --version command to check its version.
Before calling the API, you must configure environment variables to allow the application to read your access credentials: VMS_AK_ENV (AccessKey ID) and VMS_SK_ENV (AccessKey Secret). For configuration methods, see Configure environment variables in Linux, macOS, and Windows.
Install dependencies
Run the following commands in sequence to install dependencies:
pip3 install alibabacloud_credentials
pip install alibabacloud_dytnsapi20230101
pip3 install alibabacloud_tea_consoleDownload the sample code
On the Parameters tab, set the required parameters. In this example, Region is set to China (Hangzhou).
On the SDK Sample Code tab, select Python and click Download Project.
Decompress the package and go to the alibabacloud_sample directory.
Run the code
Run the following command:
python sample.pyThe following sample result is returned:
{
"headers":{
"access-control-allow-origin":"*",
"date":"Wed, 24 Apr 2024 09:27:47 GMT",
"content-length":"128",
"keep-alive":"timeout=25",
"x-acs-request-id":"7250E964-DFF7-5343-9CB6-4BA94AAD320F",
"connection":"keep-alive",
"content-type":"application/json;charset=utf-8",
"etag":"1Yd7WQjEOnVh24d2GHVjkIQ8",
"access-control-expose-headers":"*",
"x-acs-trace-id":"d553c17e7b941c8f9086ebe9cea4b5db"
},
"statusCode":200,
"body":{
"code":"OK",
"data":{
"carrierCode":"CTCC",
"verifyResult":1
},
"message":"OK",
"requestId":"7250E964-DFF7-5343-9CB6-4BA94AAD320F"
}
}