-
Notifications
You must be signed in to change notification settings - Fork 13
/
facebook.go
50 lines (42 loc) · 885 Bytes
/
facebook.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
package social
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
)
type facebookUserData struct {
FirstName string
LastName string
Avatar string
Email string
}
type Facebook struct {
}
func (p *Facebook) GetUserData(token string) (*UserData, error) {
resp, err := http.Get("https://graph.facebook.com/me?access_token=" +
url.QueryEscape(token))
defer func(body io.ReadCloser) {
_ = body.Close()
}(resp.Body)
if err != nil {
return nil, err
}
// read all response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
facebookUser := &facebookUserData{}
err = json.Unmarshal(body, facebookUser)
if err != nil {
panic(err.Error())
}
return &UserData{
FirstName: facebookUser.FirstName,
LastName: facebookUser.LastName,
Avatar: facebookUser.Avatar,
Email: facebookUser.Email,
}, nil
}