Go
最近更新时间:11/9/2023, 4:21:50 PM作者:andrewren(任豪)
简介
- 本文以 GO SDK 为例,介绍如何使用、调试并接入工业云 OpenAPI。
依赖环境
- Go 1.13版本及以上。
- 获取安全凭证。安全凭证包含 SecretId 及 SecretKey 两部分。SecretId 用于标识 API 调用者的身份,SecretKey 用于加密签名字符串和服务器端验证签名字符串的密钥,获取密钥需向工业云申请获取。
!您的安全凭证切勿泄露他人。
- 获取调用地址。调用地址(endpoint)一般形式为
*.{工业环境域名}
,模块的调用地址有一定区别,例如,商品的调用地址为product-mgmt-api.{工业环境域名}
,具体调用地址可参考对应的 API 文档。
安装 SDK
通过 go get 安装
待提供
通过源码安装
前往 SDK 托管地址 下载最新代码,解压后使用。
使用 SDK
每个接口都有一个对应的 Request 结构和一个 Response 结构。例如,商品的查询商品列表接口 DescribeProducts 有对应的请求结构体 DescribeProductRequest 和返回结构体 DescribeProductResponse。
下面以查询商品列表接口为例,介绍 SDK 的基础用法。出于演示目的,有一些非必要的内容也在示例中,以尽量展示 SDK 常用的功能,但也显得臃肿,在实际编写代码使用 SDK 的时候,应尽量简化。
示例1:商品列表(DescribeProducts)
package main
import (
"fmt"
"sdk-go/client/product"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/regions"
)
func main() {
credential := common.NewCredential("secretId",
"secretKey")
cpf := profile.NewClientProfile()
cpf.HttpProfile.ReqMethod = "POST"
cpf.HttpProfile.ReqTimeout = 30
cpf.HttpProfile.Scheme = "HTTP"
cpf.HttpProfile.Endpoint = "product-mgmt-app-api.delivery.wedev.cool"
cpf.SignMethod = "TC3-HMAC-SHA256"
cpf.Language = "zh-CN"
client, _ := product.NewClient(credential, regions.Guangzhou, cpf)
request := product.NewDescribeProductsRequest()
response, err := client.DescribeProducts(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return
}
if err != nil {
panic(err)
}
fmt.Printf("%v, %+v", *response.Response.RequestId, response.Response.ProductInfos)
}
在 cmd 窗口执行即可:
!此过程会真实发送请求,请不要测试消费等接口,以免造成财产损失。
更多示例
您可以在 SDK 中的examples
目录下获取更多详细的示例。