-
Notifications
You must be signed in to change notification settings - Fork 14
/
google.go
56 lines (46 loc) · 1.05 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
package social
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"net/url"
)
type googleUserData struct {
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(token string) (*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 := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err.Error())
}
googleUser := &googleUserData{}
err = json.Unmarshal(body, googleUser)
if err != nil {
panic(err.Error())
}
return &UserData{
FirstName: googleUser.FirstName,
LastName: googleUser.LastName,
Avatar: googleUser.Picture,
Email: googleUser.Email,
}, nil
}