-
Notifications
You must be signed in to change notification settings - Fork 13
/
google.go
46 lines (38 loc) · 918 Bytes
/
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
package social
import (
"encoding/json"
"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))
if err != nil {
return nil, err
}
// read all response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err.Error())
}
googleUserData := &googleUserData{}
err = json.Unmarshal(body, googleUserData)
if err != nil {
panic(err.Error())
}
return &UserData{
FirstName: googleUserData.FirstName,
LastName: googleUserData.LastName,
Avatar: googleUserData.Picture,
Email: googleUserData.Email,
}, nil
}