COTOHA API Portal の使用例です。
次と同じことを Golang で行いました。
ライブラリーのインストール
go get github.com/joho/godotenv
access_token.go
// ---------------------------------------------------------------
//
// access_token.go
//
// Feb/24/2020
// ---------------------------------------------------------------
package main
import (
"fmt"
"os"
"strings"
"io/ioutil"
"net/http"
"encoding/json"
"github.com/joho/godotenv"
)
// ---------------------------------------------------------------
func main() {
fmt.Fprintf (os.Stderr,"*** 開始 ***\n")
err := godotenv.Load()
if err != nil {
fmt.Println("Error loading .env file")
}
unit_aa := make (map[string]interface{})
unit_aa["grantType"] = "client_credentials"
unit_aa["clientId"] = os.Getenv("CLIENT_ID")
unit_aa["clientSecret"] = os.Getenv("CLIENT_SECRET")
str_json, _ := json.Marshal(unit_aa)
url_target := os.Getenv("ACCESS_TOKEN_PUBLISH_URL")
response, error := http.Post(url_target,"application/json",
strings.NewReader(string(str_json)))
if error != nil {
fmt.Println("Request error:", error)
}
bb, err := ioutil.ReadAll(response.Body)
if err == nil {
fmt.Println(string(bb))
}
fmt.Fprintf (os.Stderr,"*** 終了 ***\n")
}
// ---------------------------------------------------------------
実行コマンド
go run access_token.go