Go
Scenarios
To use Go to call an API through App authentication, obtain the Go SDK, create a project, and then call the API by referring to the API calling example.
Prerequisites
- You have obtained API calling information. For details, see Preparations.
- You have installed the development tool and Go development environment. For details, see Preparations.
- You have installed the Go plug-in on IntelliJ IDEA. Otherwise, install it according to Figure 1.
Obtaining the SDK
Old version: Log in to the ROMA Connect console, choose API Connect > API Calling > SDKs, and download the SDK.
New version: Log in to the ROMA Connect console, choose API Connect > Credentials > SDKs, and download the SDK.
The following shows the directory structure after the decompression.
|
Name |
Description |
|---|---|
|
core\escape.go |
SDK code |
|
core\signer.go |
|
|
demo.go |
Sample code |
Creating a Project
- Start IntelliJ IDEA and choose File > New > Project.
On the displayed New Project page, choose Go and click Next.
Figure 2 New Project
- Click ..., select the directory where the SDK is decompressed, and click Finish.
Figure 3 Selecting the SDK directory after decompression
- View the directory structure of the project.
Figure 4 Directory structure of the new project
Modify the parameters in sample code demo.go as required. For details about the sample code, see API Calling Example.
API Calling Example
- Import the Go SDK (signer.go) to the project.
import "apig-sdk/go/core"
- Generate a signer and enter the key and secret of the authorized credential. For details about how to obtain the information, see Obtaining API Calling Information.
s := core.Signer{ // Directly writing AK/SK in code is risky. For security, encrypt your AK/SK and store them in the configuration file or environment variables. // In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK. Key: os.Getenv("CLOUD_SDK_AK"), Secret:os.Getenv("CLOUD_SDK_SK"), } - Generate a request, and specify the domain name, method, request URL, query, and body. For details about how to obtain the information, see Obtaining API Calling Information.
r, _ := http.NewRequest("POST", "http://c967a237-cd6c-470e-906f-a8655461897e.apigw.exampleRegion.com/api?a=1&b=2", ioutil.NopCloser(bytes.NewBuffer([]byte("foo=bar")))) - Add the x-stage header to the request to specify the environment. Add other headers to sign as required.
r.Header.Add("x-stage", "RELEASE") - Execute the following function to add the X-Sdk-Date and Authorization headers for signing:
s.Sign(r)
- If the subdomain name allocated by the system is used to access the API of HTTPS requests, ignore the certificate verification. Otherwise, skip this step.
client:=&http.Client{ Transport:&http.Transport{ TLSClientConfig:&tls.Config{InsecureSkipVerify:true}, }, } - Access the API and view the access result.
resp, err := http.DefaultClient.Do(r) body, err := ioutil.ReadAll(resp.Body)
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.
