LoginSignup
0
0

More than 5 years have passed since last update.

Domo の アクセストークンを取得する (go)

Last updated at Posted at 2018-02-24

こちらのプログラムを go で書きました。
Python で Domo の アクセストークンを取得する

main_get_token.go
// ----------------------------------------------------------------
/*
    main_get_token.go

                    Feb/24/2018
*/
// ----------------------------------------------------------------
package main
import (
    "fmt"
    "os"
)

// ----------------------------------------------------------------
func main() {
    fmt.Fprintf (os.Stderr,"*** 開始 ***\n")

    access_token := get_token_proc ()

    fmt.Fprintf(os.Stderr,"%s\n", access_token)

    fmt.Fprintf (os.Stderr,"*** 終了 ***\n")
}

// ----------------------------------------------------------------
get_token.go
// ----------------------------------------------------------------
/*
    get_token.go

                    Feb/24/2018
*/
// ----------------------------------------------------------------
package main
import (
    "fmt"
    "os"
    "log"
    "io/ioutil"
    "net/http"
    "encoding/json"
)

// ----------------------------------------------------------------
func get_token_proc () string {

    access_token := ""

    user := "0ab7adx2-1ep8-11bb-a622-67g91f578e56"
    password := "cd295ed5dd11eb70914f8fx61523e8370b9e72d23c084a1209154ad6d203b3ba"

    url := "https://" + user + ":" + password + "@api.domo.com/oauth/token?grant_type=client_credentials&scope=data"

    resp, err := http.Get (url)

    if err != nil {
        log.Fatalln(err)
        fmt.Printf("%s", err)
        os.Exit(1) 
    } else {
        defer resp.Body.Close()
        contents, err := ioutil.ReadAll(resp.Body)
        if err != nil {
            fmt.Printf("%s", err)
            os.Exit(1)
            }
        json_str := string(contents)
//      fmt.Printf("%s\n", json_str)
        var data map[string]interface{}
        if err := json.Unmarshal([]byte(json_str), &data); err != nil {
            panic(err)
            }

    access_token = data["access_token"].(string)
    }

    return access_token
}

// ----------------------------------------------------------------
Makefile
main_get_token: main_get_token.go get_token.go
    go build main_get_token.go get_token.go
clean:
    rm -f main_get_token
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