Go Signing Guide
This section uses IntelliJ IDEA as an example to describe how to integrate the Go SDK for API request signing. You can import the sample project in the code package, and integrate the signing SDK into your application by referring to the API calling example.
Preparing the IDEA Development Environment
- Download IntelliJ IDEA 2022.2.1 or later from the IntelliJ IDEA official website and install it.
- Download the Go 1.14 or later from the Go official website and install it.
- You have installed the Go plug-in on IntelliJ IDEA. Otherwise, install it according to Figure 1.
Obtaining the SDK
Log in to the APIG console and choose Help Center > SDK Process Flow. Then download the SDK.
Develop your application using the SDK and sample code.
|
Name |
Description |
|---|---|
|
core\escape.go |
Used for escaping special characters. |
|
core\signer.go |
Signing SDK |
|
demo.go |
Sample code |
Creating a Project
- Start IntelliJ IDEA and choose File > New > Project.
In the displayed New Project dialog box, set Name to the name of the folder in the SDK package, Location to the decompression path of the folder, and Language to Go, and click Create.
Figure 2 Go
- View the directory structure shown in the following figure.
Figure 3 Directory structure of the new project go
Modify the parameters in sample code demo.go as required. For details about the sample code, see Request Signing and API Calling.
Request Signing and API Calling
After the calling information is modified, the sample code can be directly called. For details about the calling information, see Preparations.
- Import the Go SDK (signer.go) to the project.
import "./core"
- Generate a new signer and enter the AK and SK.
- In this example, the AK and SK stored in the environment variables are used. Specify the environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment first. The following uses Linux as an example to describe how to set the obtained AK/SK as environment variables.
- Open the terminal and run the following command to open the environment variable configuration file:
- Set environment variables, save the file, and exit the editor.
export CLOUD_SDK_AK="Obtained AK" export CLOUD_SDK_SK="Obtained SK"
- Run the following command to apply the modification:
- Generate a new signer and enter the configured environment variables.
s := core.Signer{ Key: os.Getenv("CLOUD_SDK_AK"), Secret: os.Getenv("CLOUD_SDK_SK"), }
- In this example, the AK and SK stored in the environment variables are used. Specify the environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment first. The following uses Linux as an example to describe how to set the obtained AK/SK as environment variables.
- Generate a new request, and specify the domain name, method, request URI, and body.
//The following example shows how to set the request URL and parameters to query a VPC list. //Add a body if you have specified the PUT or POST method. Special characters, such as the double quotation mark ("), contained in the body must be escaped. r, _ := http.NewRequest("GET", "https://service.region.example.com/v1/{project_id}/vpcs?a=1", ioutil.NopCloser(bytes.NewBuffer([]byte("")))) - Add other headers required for request signing or other purposes. For example, add the X-Project-Id header in multi-project scenarios or the X-Domain-Id header for a global service.
/Add header parameters, for example, X-Domain-Id for invoking a global service and X-Project-Id for invoking a project-level service. r.Header.Add("X-Project-Id", "xxx") - Execute the following function to add the X-Sdk-Date and Authorization headers for signing:
s.Sign(r)
- 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.
