LoginSignup
0
0

More than 3 years have passed since last update.

COTOHA アクセストークンの取得 (Golang)

Posted at

COTOHA API Portal の使用例です。

次と同じことを Golang で行いました。

COTOHA アクセストークンの取得

ライブラリーのインストール

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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0