Created
November 24, 2020 08:34
-
-
Save developer-guy/7e7b602d57bfbcf6abb55b07cd68e9f5 to your computer and use it in GitHub Desktop.
Go oauth2 sample for Github
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"golang.org/x/oauth2" | |
"golang.org/x/oauth2/endpoints" | |
"log" | |
"net" | |
"net/http" | |
"os/exec" | |
"runtime" | |
) | |
func main() { | |
codeCh, err := startWebServer() | |
if err != nil { | |
log.Fatal("Unable to start a web server.detail:", err.Error()) | |
} | |
ctx := context.Background() | |
// Set ClientId and ClientSecret to | |
oauthConf := &oauth2.Config{ | |
ClientID: "cfe0990674f9fe389fd2", | |
ClientSecret: "78b7a026ab42716b0e1afa3b4fbc64bbc2040c6f", | |
// select level of access you want https://developer.github.com/v3/oauth/#scopes | |
Scopes: []string{"user:email", "repo"}, | |
Endpoint: endpoints.GitHub, | |
} | |
// Redirect user to consent page to ask for permission | |
// for the scopes specified above. | |
authURL := oauthConf.AuthCodeURL("state-token", oauth2.AccessTypeOffline) | |
err = openURL(authURL) | |
if err != nil { | |
log.Fatalf("Unable to open authorization URL in web server: %v", err) | |
} else { | |
fmt.Println("Your browser has been opened to an authorization URL.", | |
" This program will resume once authorization has been provided.") | |
fmt.Println(authURL) | |
} | |
// Wait for the web server to get the code. | |
code := <-codeCh | |
tok, err := oauthConf.Exchange(ctx, code) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("Token:", tok.AccessToken) | |
} | |
// startWebServer starts a web server that listens on http://localhost:8080. | |
// The webserver waits for an oauth code in the three-legged auth flow. | |
func startWebServer() (codeCh chan string, err error) { | |
listener, err := net.Listen("tcp", "localhost:8080") | |
if err != nil { | |
return nil, err | |
} | |
codeCh = make(chan string) | |
go http.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
code := r.FormValue("code") | |
codeCh <- code // send code to OAuth flow | |
_ = listener.Close() | |
w.Header().Set("Content-Type", "text/plain") | |
_, _ = fmt.Fprintf(w, "Received code: %v\r\nYou can now safely close this browser window.", code) | |
})) | |
return codeCh, nil | |
} | |
func openURL(url string) error { | |
var err error | |
switch runtime.GOOS { | |
case "linux": | |
err = exec.Command("xdg-open", url).Start() | |
case "windows": | |
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", "http://localhost:4001/").Start() | |
case "darwin": | |
err = exec.Command("open", url).Start() | |
default: | |
err = fmt.Errorf("cannot open URL %s on this platform", url) | |
} | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment