Help us understand the problem. What is going on with this article?

App Engine for Go からgithub APIを叩く

More than 5 years have passed since last update.

アクセストークンの生成

https://github.com/settings/tokens の手順に従いアクセストークンを生成する

Github APIを叩く!

App EngineからHttp Requestを投げる時はurlfetch apiを利用する
Authorizationにアクセストークンを入れるのを忘れずに!

Markdown API を叩いている雑な例

import (
    "bytes"
    "encoding/json"
    "io/ioutil"
    "net/http"

    "google.golang.org/appengine"
    "google.golang.org/appengine/urlfetch"
)

type MarkdownPostParam struct {
    Text    string `json:"text"`
    Mode    string `json:"mode"`
    Context string `json:"context"`
}

func init() {
    http.HandleFunc("/", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {
    ctx := appengine.NewContext(r)

    param := MarkdownPostParam{
        Text:    "Hello Markdown,
        Mode:    "gfm",
        Context: "github/gollum",
    }
    paramBytes, err := json.Marshal(param)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    client := urlfetch.Client(ctx)
    req, err := http.NewRequest("POST", "https://api.github.com/markdown", bytes.NewReader(paramBytes))
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    req.Header.Set("Content-Type", "application/json")
    req.Header.Set("Authorization", fmt.Sprintf("token %s", "{your token}"))
    resp, err := client.Do(req)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

        body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
}
sinmetal
GCPUG Admin https://gcpug.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away