/
oauth.go
49 lines (44 loc) · 1.1 KB
/
oauth.go
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/gmail/v1"
)
func GetService(token *oauth2.Token) *gmail.Service {
config := GetConfig()
ctx := context.Background()
client := config.Client(ctx, token)
srv, err := gmail.New(client)
if err != nil {
log.Fatalf("Unable to retrieve gmail Client %v", err)
}
return srv
}
func GetConfig() *oauth2.Config {
b, err := ioutil.ReadFile("client_id.json")
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
config, err := google.ConfigFromJSON(b, gmail.GmailModifyScope)
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
return config
}
// saveToken uses a file path to create a file and store the
// token in it.
func saveToken(file string, token *oauth2.Token) {
fmt.Printf("Saving credential file to: %s\n", file)
f, err := os.Create(file)
if err != nil {
log.Fatalf("Unable to cache oauth token: %v", err)
}
defer f.Close()
json.NewEncoder(f).Encode(token)
}