This topic describes how to use the server SDK for Go provided by ApsaraVideo Live and provides relevant sample code. This topic uses the API operation that adds a domain name as an example to show you how to call an ApsaraVideo Live API operation.
Prerequisites
The latest version of Go is installed on the server.
Procedure
Run the following command to install the server SDK for Go:
go get github.com/aliyun/alibaba-cloud-sdk-go/sdk
Create a configuration file named
config.ini
and place it in the conf directory. Include your AccessKey ID and AccessKey secret in the configuration file. Example:access_key_id = YOUR_ACCESS_KEY_ID access_key_secret = YOUR_ACCESS_KEY_SECRET
Replace
YOUR_ACCESS_KEY_ID
andYOUR_ACCESS_KEY_SECRET
with your actual AccessKey ID and AccessKey secret.You must specify the AccessKey ID and AccessKey secret in your project. We recommend that you do so by using startup parameters and environment variables.
For more information about how to obtain the AccessKey ID and AccessKey secret, see Obtain an AccessKey pair.
Initialize the client.
package main
import (
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/live"
"github.com/go-ini/ini"
)
func main() {
cfg, err := ini.Load("conf/config.ini")
if err != nil {
fmt.Print(err.Error())
}
// The AccessKey pair of an Alibaba Cloud account has access permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M.
// We recommend that you not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised.
// In this example, the AccessKey pair is obtained from the configuration file to authenticate API accesses.
accessKeyID := cfg.Section("").Key("access_key_id").String()
accessKeySecret := cfg.Section("").Key("access_key_secret").String()
liveClient, err := live.NewClientWithAccessKey("cn-shanghai", accessKeyID, accessKeySecret)
if err != nil {
fmt.Print(err.Error())
}
Call API operations. The following sample code uses the AddLiveDomain operation as an example.
package main
import (
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/live"
"github.com/go-ini/ini"
)
func main() {
cfg, err := ini.Load("conf/config.ini")
if err != nil {
fmt.Print(err.Error())
}
// The AccessKey pair of an Alibaba Cloud account has access permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M.
// We recommend that you not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised.
// In this example, the AccessKey pair is obtained from the configuration file to authenticate API accesses.
accessKeyID := cfg.Section("").Key("access_key_id").String()
accessKeySecret := cfg.Section("").Key("access_key_secret").String()
liveClient, err := live.NewClientWithAccessKey("cn-shanghai", accessKeyID, accessKeySecret)
if err != nil {
fmt.Print(err.Error())
}
request := live.CreateAddLiveDomainRequest()
request.Scheme = "https"
request.LiveDomainType = "liveVideo"
request.Region = "XXX"
request.DomainName = "XXX "
response, err := liveClient.AddLiveDomain(request)
if err != nil {
fmt.Print(err.Error())
}
fmt.Printf("response is %#v\n", response)
}
For more information, see List of operations by function.