-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
81 lines (70 loc) · 2.11 KB
/
Copy pathauth.go
File metadata and controls
81 lines (70 loc) · 2.11 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"context"
"fmt"
"log"
"net"
"net/http"
"os/exec"
"runtime"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/drive/v3"
)
// RunAuth performs the one-time OAuth2 authorization flow and prints the refresh token.
func RunAuth(clientID, clientSecret string) {
cfg := oauthConfig(clientID, clientSecret, "")
// Start a local server to catch the redirect
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatalf("listen: %v", err)
}
port := listener.Addr().(*net.TCPAddr).Port
redirectURL := fmt.Sprintf("http://localhost:%d", port)
cfg.RedirectURL = redirectURL
codeCh := make(chan string, 1)
srv := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("code")
fmt.Fprintln(w, "<h2>Done! You can close this tab.</h2>")
codeCh <- code
})}
go srv.Serve(listener) //nolint:errcheck
authURL := cfg.AuthCodeURL("state", oauth2.AccessTypeOffline, oauth2.ApprovalForce)
fmt.Println("Opening browser for Google authorization...")
openBrowser(authURL)
fmt.Println("Waiting for authorization...")
code := <-codeCh
srv.Shutdown(context.Background()) //nolint:errcheck
token, err := cfg.Exchange(context.Background(), code)
if err != nil {
log.Fatalf("token exchange: %v", err)
}
fmt.Println("\n✓ Authorization successful!")
fmt.Println("\nAdd this to your .env file:")
fmt.Printf("GOOGLE_REFRESH_TOKEN=%s\n", token.RefreshToken)
}
func oauthConfig(clientID, clientSecret, redirectURL string) *oauth2.Config {
return &oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURL: redirectURL,
Scopes: []string{drive.DriveScope},
Endpoint: google.Endpoint,
}
}
func openBrowser(url string) {
var cmd string
var args []string
switch runtime.GOOS {
case "darwin":
cmd, args = "open", []string{url}
case "linux":
cmd, args = "xdg-open", []string{url}
default:
fmt.Printf("Open this URL in your browser:\n%s\n", url)
return
}
if err := exec.Command(cmd, args...).Start(); err != nil {
fmt.Printf("Open this URL in your browser:\n%s\n", url)
}
}