From d6e365f564fcf9c3262f5dcc3317c9ecd14cfb3f Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Sat, 8 Oct 2022 16:10:40 +0000 Subject: [PATCH] fix: github oauth failed. --- .vscode/settings.json | 3 +++ api/oauth_github_callback.go | 27 ++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9d83bf0 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "files.autoSave": "onFocusChange" +} \ No newline at end of file diff --git a/api/oauth_github_callback.go b/api/oauth_github_callback.go index 0f56dce..dcc68e4 100644 --- a/api/oauth_github_callback.go +++ b/api/oauth_github_callback.go @@ -3,13 +3,26 @@ package main import ( "encoding/json" "fmt" - "golang.org/x/oauth2" "io/ioutil" "net/http" + + "golang.org/x/oauth2" ) 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() 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()) 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 { fmt.Fprintf(w, "Error: %s", err.Error()) 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() contents, err := ioutil.ReadAll(resp.Body)