fix: github oauth failed.

This commit is contained in:
Ivan Li 2022-10-08 16:10:40 +00:00
parent 4d1d52095f
commit d6e365f564
2 changed files with 27 additions and 3 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"files.autoSave": "onFocusChange"
}

View File

@ -3,13 +3,26 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"golang.org/x/oauth2"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"golang.org/x/oauth2"
) )
func githubGetPrimaryEmail(accessToken string) (string, error) { func githubGetPrimaryEmail(accessToken string) (string, error) {
resp, err := http.Get("https://api.github.com/user/emails?access_token=" + accessToken) client := &http.Client{}
req, err := http.NewRequest("GET", "https://api.github.com/user/emails", nil)
if err != nil {
logger.Errorf("Error creating github email request: %s", err.Error())
return "", err
}
req.Header.Add("Accept", "application/vnd.github+json")
req.Header.Add("Authorization", "Bearer " + accessToken)
resp, err := client.Do(req)
if err != nil {
logger.Errorf("Error adding github auth token: %s", err.Error())
return "", err
}
defer resp.Body.Close() defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body) contents, err := ioutil.ReadAll(resp.Body)
@ -55,12 +68,20 @@ func githubCallbackHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Error: %s", err.Error()) fmt.Fprintf(w, "Error: %s", err.Error())
return return
} }
client := &http.Client{}
resp, err := http.Get("https://api.github.com/user?access_token=" + token.AccessToken) req, err := http.NewRequest("GET", "https://api.github.com/user", nil)
if err != nil { if err != nil {
fmt.Fprintf(w, "Error: %s", err.Error()) fmt.Fprintf(w, "Error: %s", err.Error())
return return
} }
req.Header.Add("Accept", "application/vnd.github+json")
req.Header.Add("Authorization", "Bearer " + token.AccessToken)
resp, err := client.Do(req)
if err != nil {
logger.Errorf("Error adding github auth token: %s", err.Error())
return
}
defer resp.Body.Close() defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body) contents, err := ioutil.ReadAll(resp.Body)