/
fb.go
59 lines (51 loc) · 1.29 KB
/
fb.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
59
package fb
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"github.com/ranveerkunal/admin"
)
const (
EndPoint = "https://graph.facebook.com/v2.0"
)
var (
ClientID = flag.String("fbClientID", os.Getenv("FB_CLIENT_ID"), "")
ClientSecret = flag.String("fbClientSecret", os.Getenv("FB_CLIENT_SECRET"), "")
VerifyToken = flag.String("verifyToken", os.Getenv("VERIFY_TOKEN"), "")
SubURL = fmt.Sprintf("%s/%s/subscriptions", EndPoint, *ClientID)
)
type SignedRequest struct {
Code string `json:"code"`
Algorithm string `json:"algorithm"`
IssuedAt uint64 `json:"issued_at"`
UserId string `json:"user_id"`
}
type Stub struct {
verifyToken string
accessToken []string
}
func NewStub() *Stub {
return &Stub{
verifyToken: *VerifyToken,
}
}
func (fbs *Stub) OAuth(s *admin.Status) error {
v := url.Values{}
v.Add("client_id", *ClientID)
v.Add("client_secret", *ClientSecret)
v.Add("grant_type", "client_credentials")
res, err := http.Get(fmt.Sprintf("%s/oauth/access_token?%s", EndPoint, v.Encode()))
ok := err == nil && res.StatusCode == http.StatusOK
s.Log["FB Access Token"] = admin.NewLog(ok, res)
if !ok {
return err
}
body, err := ioutil.ReadAll(res.Body)
fbs.accessToken = strings.Split(string(body), "=")
res.Body.Close()
return err
}