-
Notifications
You must be signed in to change notification settings - Fork 13
/
google.go
58 lines (48 loc) · 1.12 KB
/
google.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
50
51
52
53
54
55
56
57
58
package social
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
)
type googleUserData struct {
ID string `json:"id"`
Email string `json:"email"`
LastName string `json:"family_name"`
FirstName string `json:"given_name"`
Picture string `json:"picture"`
}
type Google struct {
}
func (p *Google) GetUserData(_ context.Context, token string, _ bool) (*UserData, error) {
resp, err := http.Get("https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=" +
url.QueryEscape(token))
defer func(body io.ReadCloser) {
_ = body.Close()
}(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New("Status: " + resp.Status)
}
// read all response body
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err.Error())
}
googleUser := &googleUserData{}
err = json.Unmarshal(body, googleUser)
if err != nil {
panic(err.Error())
}
return &UserData{
ID: googleUser.ID,
FirstName: googleUser.FirstName,
LastName: googleUser.LastName,
Avatar: googleUser.Picture,
Email: googleUser.Email,
}, nil
}