-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthenticate.go
44 lines (36 loc) · 1.06 KB
/
authenticate.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
package helpers
import (
"context"
"fmt"
"io/ioutil"
"github.com/byuoitav/common/log"
"golang.org/x/oauth2/google"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/option"
)
//AuthenticateClient authenticates the client and returns a calendar service object
func AuthenticateClient(credentials string, userEmail string) (*calendar.Service, error) {
ctx := context.Background()
data, err := ioutil.ReadFile(credentials)
if err != nil {
log.L.Errorf("Can't read project key file | %s", err.Error())
return nil, err
}
log.L.Info("Signing JWT")
conf, err := google.JWTConfigFromJSON(data, "https://www.googleapis.com/auth/calendar")
if err != nil {
log.L.Errorf("Can't sign JWT | %s", err.Error())
return nil, err
}
log.L.Info(userEmail)
if userEmail != "" {
conf.Subject = userEmail
}
ts := conf.TokenSource(ctx)
log.L.Info("Getting authorization")
service, err := calendar.NewService(ctx, option.WithTokenSource(ts))
if err != nil {
return nil, fmt.Errorf("Can't make calendar service | %s", err.Error())
}
return service, nil
}