The process of integrating Alibaba Cloud Darabonba SDK consists of three steps: configure a credential, import the SDK to your project, and write the code for calling API operations. This topic describes how to integrate Alibaba Cloud Darabonba SDK. In this example, Alibaba Cloud Darabonba SDK for Go
is used to call the SendSms operation of Short Message Service (SMS) to send a text message.
Prerequisites
Go 1.10.X or later is installed.
The core package of Alibaba Cloud Darabonba SDK is installed.
go get github.com/alibabacloud-go/darabonba-openapi/v2/client
Step 1: Configure a credential
An AccessKey pair is required if you use Alibaba Cloud Darabonba SDK to call API operations. For more information, see Credential. To prevent AccessKey pair leaks, you can record the AccessKey pair in environment variables. For information about other security solutions, see Credential security solutions. In this example, the ALIBABA_CLOUD_ACCESS_KEY_ID
and ALIBABA_CLOUD_ACCESS_KEY_SECRET
environment variables are configured.
Linux and macOS
Create the
.bash_profile
file.NoteIf you configure environment variables for the first time, you can run the
touch ~/.bash_profile
command to create the configuration file. If the configuration file already exists, run thevim ~/.bash_profile
command to edit the configuration file.touch ~/.bash_profile vim ~/.bash_profile
Configure the
.bash_profile
file.# Replace <ACCESS_KEY_ID> and <ACCESS_KEY_SECRET> in the following commands with your AccessKey ID and AccessKey secret. # Replace the variable names based on your business requirements, such as OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. export ALIBABA_CLOUD_ACCESS_KEY_ID=<ACCESS_KEY_ID> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<ACCESS_KEY_SECRET>
Save the file after the configuration.
Use the
.bash_profile
file.# Run the following command in the configuration file. source ~/.bash_profile # Check whether the configuration takes effect. echo $ALIBABA_CLOUD_ACCESS_KEY_ID
If the valid AccessKey pair is returned, the configuration is successful.
Windows
In this example, the
ALIBABA_CLOUD_ACCESS_KEY_ID
andALIBABA_CLOUD_ACCESS_KEY_SECRET
environment variables are configured.Use GUI
Procedure
If you want to use GUI to configure environment variables in Windows 10, perform the following steps:
On the Windows desktop, right-click This PC and select Properties. On the page that appears, click Advanced system settings. In the System Properties dialog box, click Environment Variables on the Advanced tab. In the Environment Variables dialog box, click New in the User variables or System variables section. Then, configure the variables described in the following table.
Variable
Example
AccessKey ID
Variable name: ALIBABA_CLOUD_ACCESS_KEY_ID
Variable value: LTAI4GDty8ab9W4Y1D****
AccessKey Secret
Variable name: ALIBABA_CLOUD_ACCESS_KEY_SECRET
Variable value: IrVTNZNy5yQelTETg0cZML3TQn****
Check whether the configuration is successful.
On the Windows desktop, click Start or press Win + R. In the Run dialog box, enter cmd. Then, click OK or press the Enter key. On the page that appears, run the
echo %ALIBABA_CLOUD_ACCESS_KEY_ID%
andecho %ALIBABA_CLOUD_ACCESS_KEY_SECRET%
commands. If the valid AccessKey pair is returned, the configuration is successful.
Use CMD
Procedure
Open a Command Prompt window as an administrator and run the following commands to add environment variables in the operating system:
setx ALIBABA_CLOUD_ACCESS_KEY_ID LTAI4GDty8ab9W4Y1D**** /M setx ALIBABA_CLOUD_ACCESS_KEY_SECRET IrVTNZNy5yQelTETg0cZML3TQn**** /M
/M
indicates that the environment variable is of system level. You cannot use this parameter when you configure a user-level environment variable.Check whether the configuration is successful.
On the Windows desktop, click Start or press Win + R. In the Run dialog box, enter cmd. Then, click OK or press the Enter key. On the page that appears, run the
echo %ALIBABA_CLOUD_ACCESS_KEY_ID%
andecho %ALIBABA_CLOUD_ACCESS_KEY_SECRET%
commands. If the valid AccessKey pair is returned, the configuration is successful.
Use Windows PowerShell
In PowerShell, configure new environment variables. The environment variables apply to all new sessions.
[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_ID', 'LTAI4GDty8ab9W4Y1D****', [System.EnvironmentVariableTarget]::User) [System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'IrVTNZNy5yQelTETg0cZML3TQn****', [System.EnvironmentVariableTarget]::User)
Configure environment variables for all users. You must run the following commands as a administrator.
[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_ID', 'LTAI4GDty8ab9W4Y1D****', [System.EnvironmentVariableTarget]::Machine) [System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'IrVTNZNy5yQelTETg0cZML3TQn****', [System.EnvironmentVariableTarget]::Machine)
Configure temporary environment variables. The environment variables apply only to the current session.
$env:ALIBABA_CLOUD_ACCESS_KEY_ID = "LTAI4GDty8ab9W4Y1D****" $env:ALIBABA_CLOUD_ACCESS_KEY_SECRET = "IrVTNZNy5yQelTETg0cZML3TQn****"
In PowerShell, run the
Get-ChildItem env:ALIBABA_CLOUD_ACCESS_KEY_ID
andGet-ChildItem env:ALIBABA_CLOUD_ACCESS_KEY_SECRET
commands. If the valid AccessKey pair is returned, the configuration is successful.
Step 2: Import the SDK to your project
Alibaba Cloud Darabonba SDK allows you to make generic and specialized calls. For more information, see Generic calls and specialized calls. The SDK that you need to import varies based on the call type.
Specialized calls
You can visit OpenAPI Explorer, find the service that you want to use, and view the supported languages and installation methods. Then, import the SDK of the service to your project. In this example, SMS SDK is imported. Perform the following steps:
Visit the SMS SDK page.
In the All languages section, select the language that you want to use.
In the Installation Method section, select an installation method and copy the installation code.
Add the dependency to your project.
Run the following command to install SMS SDK for Go:
go get github.com/alibabacloud-go/dysmsapi-20170525/v3
Generic calls
You do not need to install the SDK of a service to make generic calls. You need to only import the github.com/alibabacloud-go/darabonba-openapi/v2/client
core package. For information about the latest version of the core package, see darabonba-openapi. Run the following command to install the core package:
go get github.com/alibabacloud-go/darabonba-openapi/v2/client
Step 3: Call an API operation
Download the sample code. For more information, see Get started with Alibaba Cloud Darabonba SDK for Go. In this example, the sample code of a specialized call is used. If you want to make a generic call, turn on Common Request when you download the sample code. For more information, see Generic calls and specialized calls.
Initialize a request client
Method 1: Use an AccessKey pair
import (
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v3/client"
"github.com/alibabacloud-go/tea/tea"
"os"
)
func CreateClient() {
config := &openapi.Config{
// Required. Specify your AccessKey ID.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Specify your AccessKey secret.
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
}
// Specify the endpoint. For more information, visit https://api.aliyun.com/product/Dysmsapi.
config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
// Initialize a request client for a specialized call.
dysmsapiClient, _err := dysmsapi20170525.NewClient(config)
}
Method 2: Use an STS token
import (
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v3/client"
"github.com/alibabacloud-go/tea/tea"
"os"
)
func CreateClient() {
config := &openapi.Config{
// Required. Specify your AccessKey ID.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Specify your AccessKey secret.
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
// Required. Specify your Security Token Service (STS) token.
SecurityToken: tea.String(os.Getenv("ALIBABA_CLOUD_SECURITY_TOKEN")),
}
// Specify the endpoint. For more information, visit https://api.aliyun.com/product/Dysmsapi.
config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
// Initialize a request client for a specialized call.
dysmsapiClient, _err := dysmsapi20170525.NewClient(config)
}
Method 3: Use the Credentials tool
import (
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v3/client"
"github.com/alibabacloud-go/tea/tea"
"github.com/aliyun/credentials-go/credentials"
)
func main() {
// Use the default credential to initialize a Credentials client.
credential, _err := credentials.NewCredential(nil)
if _err != nil {
panic(_err)
}
dysmsapiClient, _err := dysmsapi20170525.NewClient(&openapi.Config{
// Use the SDK Credentials package to configure a credential.
Credential: credential,
// To call the SMS API, initialize the Client class of the SDK package for SMS.
Endpoint: tea.String("dysmsapi.aliyuncs.com"),
})
if _err != nil {
panic(_err)
}
}
Create a request object or a runtime configuration object
In most cases, you can create a request object and specify the parameters based on your business requirements. You can also customize runtime configurations.
// Create a request object and specify the parameters.
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
PhoneNumbers: tea.String("<YOUR_VALUE>"),
SignName: tea.String("<YOUR_VALUE>"),
}
// Create a runtime configuration object.
runtime := &util.RuntimeOptions{}
Initiate a request
In the Darabonba SDK, the client of each Alibaba Cloud service provides three request methods for all API operations of the service. You can specify a request method for an API operation. The name of the request method is the name of the API operation with the first letter in lowercase. The following list describes the request methods:
<API operation name>
: uses the default runtime configurations to initiate a request.package main import ( "fmt" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v3/client" "github.com/alibabacloud-go/tea/tea" "github.com/aliyun/credentials-go/credentials" ) func main() { // Initialize a Credentials client. credentialClient, _err := credentials.NewCredential(nil) if _err != nil { panic(_err) } // Initialize a request client. smsClient, _err := dysmsapi20170525.NewClient(&openapi.Config{ // Use the SDK Credentials package to configure a credential. Credential: credentialClient, // Specify the endpoint of the cloud service. Endpoint: tea.String("dysmsapi.aliyuncs.com"), }) // Construct the request object. sendSmsRequest := &dysmsapi20170525.SendSmsRequest{} // Obtain the response object. response, _err := smsClient.SendSms(sendSmsRequest) fmt.Print(response) }
<API operation name>WithOptions
: uses the custom runtime configurations to initiate a request. For more information, see Advanced configuration.package main import ( "fmt" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v3/client" "github.com/alibabacloud-go/tea/tea" "github.com/aliyun/credentials-go/credentials" util "github.com/alibabacloud-go/tea-utils/v2/service" ) func main() { // Initialize a Credentials client. credentialClient, _err := credentials.NewCredential(nil) if _err != nil { panic(_err) } // Initialize a request client. smsClient, _err := dysmsapi20170525.NewClient(&openapi.Config{ // Use the SDK Credentials package to configure a credential. Credential: credentialClient, // Specify the endpoint of the cloud service. Endpoint: tea.String("dysmsapi.aliyuncs.com"), }) // Construct the request object. sendSmsRequest := &dysmsapi20170525.SendSmsRequest{} // Construct the runtime configuration object. runtime := &util.RuntimeOptions{} // Set the timeout period for read requests. runtime.SetReadTimeout(10000) // Set the timeout period for connection requests. runtime.SetConnectTimeout(5000) // Specify whether to automatically retry a request. runtime.SetAutoretry(true) // Obtain the response object. response, _err := smsClient.SendSmsWithOptions(sendSmsRequest,runtime) fmt.Print(response) }
<API operation name>Advance
: used for API operations that require file uploading. By default, you must specify runtime parameters. The following sample code provides an example on how to initiate a request to upload a file and call an API operation to usethe human face and body recognition feature of Visual Intelligence API (VIAPI)
:go get github.com/alibabacloud-go/facebody-20191230/v4
package main import ( "fmt" "os" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" facebody20191230 "github.com/alibabacloud-go/facebody-20191230/v4/client" util "github.com/alibabacloud-go/tea-utils/v2/service" "github.com/alibabacloud-go/tea/tea" "github.com/aliyun/credentials-go/credentials" ) func main() { // Initialize a Credentials client. credentialClient, _err := credentials.NewCredential(nil) if _err != nil { panic(_err) } // Initialize a request client. facebodyClient, _err := facebody20191230.NewClient(&openapi.Config{ // Use the SDK Credentials package to configure a credential. Credential: credentialClient, // Specify the endpoint of the cloud service. Endpoint: tea.String("facebody.cn-shanghai.aliyuncs.com"), }) if _err != nil { panic(_err) } file, _err := os.Open("<filepath>") if _err != nil { panic(_err) } // Initialize the request to call the DescribeRegions operation. detectBodyCountAdvanceRequest := &facebody20191230.DetectBodyCountAdvanceRequest{ ImageURLObject: file, } // Initialize the runtime configurations. runtime := &util.RuntimeOptions{} // Set the timeout period for read requests. runtime.SetReadTimeout(10000) // Set the timeout period for connection requests. runtime.SetConnectTimeout(5000) // Specify whether to automatically retry a request. runtime.SetAutoretry(true) response, _err := facebodyClient.DetectBodyCountAdvance(detectBodyCountAdvanceRequest, runtime) if _err != nil { panic(_err) } fmt.Print(response) }