-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgithub.go
47 lines (38 loc) · 1.08 KB
/
github.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"private-ghp/config"
"github.com/sirupsen/logrus"
)
func getGithubAccessToken(code string, config *config.Config) string {
requestBodyMap := map[string]string{
"client_id": config.Github.Client.Id,
"client_secret": config.Github.Client.Secret,
"code": code}
requestJSON, _ := json.Marshal(requestBodyMap)
req, reqerr := http.NewRequest(
"POST",
"https://github.com/login/oauth/access_token",
bytes.NewBuffer(requestJSON))
if reqerr != nil {
logrus.Error("Request creation failed")
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, resperr := http.DefaultClient.Do(req)
if resperr != nil {
logrus.Error("Request failed")
}
respbody, _ := ioutil.ReadAll(resp.Body)
type githubAccessTokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
}
var ghresp githubAccessTokenResponse
json.Unmarshal(respbody, &ghresp)
return ghresp.AccessToken
}