Created
December 14, 2023 22:00
-
-
Save eahrend/3a99bf154dc15648785076e23cf3cada to your computer and use it in GitHub Desktop.
gmail filewatcher test
This file contains 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" | |
"flag" | |
"fmt" | |
"log" | |
"net/http" | |
"net/http/httptest" | |
"os/exec" | |
"time" | |
"golang.org/x/oauth2" | |
"golang.org/x/oauth2/google" | |
gmail "google.golang.org/api/gmail/v1" | |
"google.golang.org/api/option" | |
) | |
func main() { | |
var cid string | |
var cse string | |
var topicName string | |
flag.StringVar(&cid, "clientID", "", "a string") | |
flag.StringVar(&cse, "clientSecret", "", "a string") | |
flag.StringVar(&topicName, "topicName", "", "a string") | |
flag.Parse() | |
config := &oauth2.Config{ | |
ClientID: cid, | |
ClientSecret: cse, | |
Endpoint: google.Endpoint, | |
Scopes: []string{gmail.MailGoogleComScope}, | |
} | |
ctx := context.Background() | |
c := newOAuthClient(ctx, config) | |
svc, err := gmail.NewService(ctx, option.WithHTTPClient(c)) | |
if err != nil { | |
panic(err) | |
} | |
/* | |
resp, err := svc.Users.Messages.List("me").Do() | |
if err != nil { | |
panic(err) | |
} | |
for _, v := range resp.Messages { | |
msg, err := svc.Users.Messages.Get("me", v.Id).Do() | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(msg.Snippet) | |
} | |
*/ | |
resp, err := svc.Users.Watch("me", &gmail.WatchRequest{ | |
TopicName: topicName, | |
LabelIds: []string{"INBOX"}, | |
}).Do() | |
if err != nil { | |
panic(err) | |
} | |
b, _ := resp.MarshalJSON() | |
println(string(b)) | |
} | |
func newOAuthClient(ctx context.Context, config *oauth2.Config) *http.Client { | |
token := tokenFromWeb(ctx, config) | |
return config.Client(ctx, token) | |
} | |
func tokenFromWeb(ctx context.Context, config *oauth2.Config) *oauth2.Token { | |
ch := make(chan string) | |
randState := fmt.Sprintf("st%d", time.Now().UnixNano()) | |
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | |
if req.URL.Path == "/favicon.ico" { | |
http.Error(rw, "", 404) | |
return | |
} | |
if req.FormValue("state") != randState { | |
log.Printf("State doesn't match: req = %#v", req) | |
http.Error(rw, "", 500) | |
return | |
} | |
if code := req.FormValue("code"); code != "" { | |
fmt.Fprintf(rw, "<h1>Success</h1>Authorized.") | |
rw.(http.Flusher).Flush() | |
ch <- code | |
return | |
} | |
log.Printf("no code") | |
http.Error(rw, "", 500) | |
})) | |
defer ts.Close() | |
config.RedirectURL = ts.URL | |
authURL := config.AuthCodeURL(randState) | |
go openURL(authURL) | |
log.Printf("Authorize this app at: %s", authURL) | |
code := <-ch | |
log.Printf("Got code: %s", code) | |
token, err := config.Exchange(ctx, code) | |
if err != nil { | |
log.Fatalf("Token exchange error: %v", err) | |
} | |
return token | |
} | |
func openURL(url string) { | |
try := []string{"xdg-open", "google-chrome", "open"} | |
for _, bin := range try { | |
err := exec.Command(bin, url).Run() | |
if err == nil { | |
return | |
} | |
} | |
log.Printf("Error opening URL in browser.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment