-
Notifications
You must be signed in to change notification settings - Fork 1
/
fb.go
93 lines (72 loc) · 2.55 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"errors"
"fmt"
"github.com/go-martini/martini"
fb "github.com/huandu/facebook"
"github.com/martini-contrib/render"
"github.com/xyproto/pinterface"
"net/http"
)
const (
// Your values goes here
appName = "fb_app_name"
appAccessToken = "fb_app_access_token"
fbTokenName = "fb_user_access_token"
fbIDName = "fb_user_id"
)
func setupFB(m *martini.ClassicMartini, r martini.Handler, userstate pinterface.IUserState) {
// Store access token for a given user
//m.Post(API+"fb/reg/:username/:token", func(params martini.Params, r render.Render) {
m.Any(API+"fb/reg/:username/:token", func(params martini.Params, r render.Render) {
username := params["username"]
token := params["token"]
if !userstate.HasUser(username) {
r.JSON(http.StatusNotFound, map[string]interface{}{"error": "no such user " + username})
return
}
users := userstate.Users()
users.Set(username, fbTokenName, token)
r.JSON(http.StatusOK, map[string]interface{}{"user id and access token set": true})
})
// Number of friends on Instagram
m.Get(API+"fb/friends/:username", func(params martini.Params, r render.Render) {
username := params["username"]
if !userstate.HasUser(username) {
r.JSON(http.StatusNotFound, map[string]interface{}{"error": "no such user " + username})
return
}
users := userstate.Users()
userAccessToken, err := users.Get(username, fbTokenName)
if err != nil {
r.JSON(http.StatusInternalServerError, map[string]interface{}{"error": "could not get fb user access token for " + username})
return
}
friends, err := facebookFriends(userAccessToken)
if err != nil {
r.JSON(http.StatusInternalServerError, map[string]interface{}{"error": "could not fetch friends from facebook: " + err.Error()})
return
} else {
r.JSON(http.StatusOK, map[string]interface{}{"friends": friends})
}
})
}
func facebookFriends(userAccessToken string) (string, error) {
// create a global App var to hold your app id and secret.
var globalApp = fb.New(appName, appAccessToken)
session := globalApp.Session(userAccessToken)
// validate access token. err is nil if token is valid.
err := session.Validate()
if err != nil {
return "", err
}
// use session to send api request with your access token.
res, _ := session.Get("/me/friends", nil)
friends := res.Get("summary")
friendCountMap := friends.(map[string]interface{})
friendCount, ok := friendCountMap["total_count"]
if !ok {
return "", errors.New("could not find total_count in result from fb")
}
return fmt.Sprintf("%v", friendCount), nil
}