GitHub appメモ
羽計歩香というアプリを作った
jwtのTokenを作るgoのコード
package main import ( "fmt" jwt "github.com/dgrijalva/jwt-go" "io/ioutil" "time" ) func main() { key, err := ioutil.ReadFile("private-key.pem") if err != nil { panic(err) } privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(key) if err != nil { panic(err) } t := jwt.New(jwt.SigningMethodRS256) t.Claims = jwt.MapClaims{ "iat": time.Now().Unix(), "exp": time.Now().Add(time.Minute * 10).Unix(), // 最大10分 "iss": <自分のIDを入れる>, } token, err := t.SignedString(privateKey) fmt.Printf("curl -i -H \"Authorization: Bearer %s\" -H \"Accept: application/vnd.github.machine-man-preview+json\" https://api.github.com/app", token) }
$ eval $(go run jwt.go)
成功するといろいろ帰ってくる
HTTP/1.1 200 OK Date: Wed, 10 Apr 2019 12:15:17 GMT Content-Type: application/json; charset=utf-8 Content-Length: 1473 Server: GitHub.com Status: 200 OK Cache-Control: public, max-age=60, s-maxage=60 Vary: Accept X-GitHub-Media-Type: github.machine-man-preview; format=json Access-Control-Allow-Origin: * ... { "id": **, "owner": { "login": "MizukiSonoko", ... }, "name": "羽計歩香go benchmark bot", "description": "Share benchmark result", "permissions": { "metadata": "read" }, "events": [ "repository" ] }
access_tokenを取得するコード
package main import ( "fmt" jwt "github.com/dgrijalva/jwt-go" "io/ioutil" "time" ) func main() { key, err := ioutil.ReadFile("private-key.pem") if err != nil { panic(err) } privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(key) if err != nil { panic(err) } t := jwt.New(jwt.SigningMethodRS256) t.Claims = jwt.MapClaims{ "iat": time.Now().Unix(), "exp": time.Now().Add(time.Minute * 10).Unix(), // 最大10分 "iss": <自分のIDを入れる>, } token, err := t.SignedString(privateKey) fmt.Printf( "curl -i -X POST -H \"Authorization: Bearer %s\" -H \"Accept: application/vnd.github.machine-man-preview+json\" https://api.github.com/app/installations/%d/access_tokens", token, <自分のinstallations_id>) }
実行
$ eval $(go run token.go)
成功するとTokenが帰ってくる
HTTP/1.1 201 Created Date: Wed, 10 Apr 2019 13:16:40 GMT Content-Type: application/json; charset=utf-8 Content-Length: 101 Server: GitHub.com Status: 201 Created Cache-Control: public, max-age=60, s-maxage=60 X-GitHub-Media-Type: github.machine-man-preview; format=json ... { "token": "v1.***", "expires_at": "2019-04-10T14:16:40Z" }