示例#1
0
文件: main.go 项目: MattHesby/meta
func main() {
	flag.StringVar(&accessToken, "token", "", "Facebook access token")
	flag.StringVar(&csvLocation, "csv", "schools.csv", "Where to save output CSV")
	flag.Parse()

	if accessToken == "" {
		log.Fatal("You must provide a Facebook access token.")
	}

	params := facebook.Params{
		"access_token": accessToken,
	}

	var groupMembers []GroupMember
	var hshackers []GroupMember
	var teendev []GroupMember
	res, err := facebook.Get(fmt.Sprintf("/%d/members", hsHackersId), params)
	if err != nil {
		log.Fatal(err)
	}
	res.DecodeField("data", &hshackers)

	res, err = facebook.Get(fmt.Sprintf("/%d/members", teenDevId), params)
	if err != nil {
		log.Fatal(err)
	}
	res.DecodeField("data", &teendev)

	groupMembers = append(groupMembers, hshackers...)
	groupMembers = append(groupMembers, teendev...)

	members := make([]Member, len(groupMembers))

	for i, m := range groupMembers {
		fmt.Printf("\rPulling student %d of %d...", i+1, len(groupMembers))

		res, err := facebook.Get(fmt.Sprintf("/%s", m.Id), params)
		if err != nil {
			log.Fatal(err)
		}
		res.Decode(&members[i])
	}
	fmt.Println()

	if err := writeCSV(members, csvLocation); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("CSV successfully written to %s.\n", csvLocation)
}
示例#2
0
文件: app.go 项目: kopain/chaparty
func WebHandler(w http.ResponseWriter, r *http.Request) {
	query := r.URL.Query()
	code := query.Get("code")

	if code == "" {
		http.Error(w, "Cannot get code from facebook.", 505)
		return
	}

	context := appengine.NewContext(r)
	client := urlfetch.Client(context)
	fb.SetHttpClient(client)

	redirectURL := "http://" + r.Host + r.URL.Path
	accessResp, err := fb.Get("/v2.4/oauth/access_token", fb.Params{
		"code":          code,
		"redirect_uri":  redirectURL,
		"client_id":     clientID,
		"client_secret": APPSECRET,
	})
	check(err, context)

	var accessToken string
	accessResp.DecodeField("access_token", &accessToken)

	paths := strings.Split(r.URL.Path, "/")
	party := paths[len(paths)-1]
	photoID := UploadPhoto(accessToken, party, context)
	redirectUrl := "https://facebook.com/photo.php?fbid=" + photoID + "&makeprofile=1&prof"
	http.Redirect(w, r, redirectUrl, 303)
}
示例#3
0
func getPages() []Page {
	// fb access token
	var accessToken = flag.String("accessToken", "", "fb access token")

	// fb element e.g. page, place
	var fbType = flag.String("fbType", "", "fb element")

	// query e.g. night,philippines
	var query = flag.String("query", "", "query")

	flag.Parse()

	log.Printf("fbType=%s, query=%q", *fbType, *query)

	res, _ := fb.Get("/search", fb.Params{
		"access_token": *accessToken,
		"type":         *fbType,
		"q":            *query,
	})

	var items []Page

	err := res.DecodeField("data", &items)
	if err != nil {
		log.Fatalln("An error has happened: ", err)
		return nil
	}

	return items
}
示例#4
0
func (user *User) VerifyFacebookToken(facebook_token string) bool {
	res, err := facebook.Get("/me", facebook.Params{
		"access_token": facebook_token,
	})

	return err == nil && res["id"] == user.FacebookId
}
示例#5
0
func processPosts(url string, total int) int {
	fmt.Println(url)
	res, err := fb.Get(url, fb.Params{
	// "access_token": ACCESS_TOKEN,
	})

	if err != nil {
		panic(err)
	}

	var posts []Post
	var paging Paging
	res.DecodeField("data", &posts)
	res.DecodeField("paging", &paging)

	if len(posts) == 0 {
		return total
	}

	u, _ := netUrl.Parse(paging.Next)
	path_without_version_prefix := u.Path[len("/v2.0"):]
	next := path_without_version_prefix + "?" + u.RawQuery
	return processPosts(next, total+len(posts))

}
示例#6
0
func mapper(results chan map[string]int, startId, endId int) int {
	var workerNum int
	for i := startId; i < endId; i = i + 35 {

		workerNum++
		go func(i int, results chan map[string]int) {
			nameMap := make(map[string]int)
			for startId := i; startId < i+35; startId++ {
				uId := "/" + strconv.Itoa(startId)
				//				fmt.Println(uId)
				res, _ := fb.Get(uId, fb.Params{
					"fields": "first_name",
					// "fields":       "bio",
					"access_token": "1156172417738306|-XmlLqK1eD7mBMNWwVxAINpFAFM",
				})
				if res != nil && res["first_name"] != nil {
					name := res["first_name"].(string)
					if val, ok := nameMap[name]; ok {
						nameMap[name] = val + 1
					} else {
						nameMap[name] = 1
					}
				}
			}
			//fmt.Println("one worker finishes")
			results <- nameMap
		}(i, results)

	}
	return workerNum
}
示例#7
0
// FaceBook Graph Query API.
func (self *FbAlbum) RunFBGraphAPI(query string) (queryResult interface{}) {
	res, err := fb.Get(query, fb.Params{
		"access_token": self.Token,
	})

	if err != nil {
		log.Fatalln("FB connect error, err=", err.Error())
	}
	return res
}
示例#8
0
// GetMe returns user profile data from facebook
func (a *Adapter) GetMe(accessToken string) (fb.Result, error) {
	return fb.Get("/me", fb.Params{
		"fields": []string{
			"id",
			"first_name",
			"last_name",
			"email",
		},
		"access_token": accessToken,
	})
}
示例#9
0
文件: facebook.go 项目: worg/go
func (self *Facebook) Me() (fb.Result, error) {
	res, err := fb.Get("me", fb.Params{
		"access_token": self.AccessToken,
	})
	return res, err
}
示例#10
0
文件: facebook.go 项目: worg/go
//Get list friends user
func (self *Facebook) GetFriends() (interface{}, error) {
	res, err := fb.Get("me/friends", fb.Params{
		"access_token": self.AccessToken,
	})
	return res, err
}
示例#11
0
func serveFacebookCallback(c web.C, w http.ResponseWriter, r *http.Request) error {
	s := getSession(c)
	state := r.FormValue("state")
	if state != oauthStateString {
		return fmt.Errorf("invalid oauth state, expected '%s', got '%s'\n", oauthStateString, state)
	}

	code := r.FormValue("code")
	token, err := oauthConf.Exchange(oauth2.NoContext, code)
	if err != nil {
		return fmt.Errorf("oauthConf.Exchange() failed with '%s'\n", err)
	}

	// Save token here.
	res, err := facebook.Get("/me", facebook.Params{
		"access_token": token.AccessToken,
	})
	if err != nil {
		return fmt.Errorf("fb.Get() failed with '%s'\n", err)
	}
	log.Printf("Logged in with Facebook user: %s\n", res["name"])
	var v struct {
		Name string `facebook:",required"`
		ID   string `facebook:",required"`
	}
	if err := res.Decode(&v); err != nil {
		return err
	}
	// Save user to DB.
	u, err := GetCreateUser(v.Name, v.ID)
	if err != nil {
		return fmt.Errorf("Could not create user %s: %s", res["name"], err)
	}
	// SAMER: Replace with fb.ExchangeToken.
	// Get the longer lasting token.
	var longLivedTokenURL = facebookoauth.Endpoint.TokenURL + "?" +
		"grant_type=fb_exchange_token&" +
		"client_id=" + oauthConf.ClientID + "&" +
		"client_secret=" + oauthConf.ClientSecret + "&" +
		"fb_exchange_token=" + token.AccessToken
	resp, err := http.Get(longLivedTokenURL)
	if err != nil {
		return fmt.Errorf("Could not extend access token for %s: %s", v.Name, err)
	}
	b, _ := ioutil.ReadAll(resp.Body)
	m, err := url.ParseQuery(string(b))
	if err != nil {
		return fmt.Errorf("Could not parse long-lived token for %s: %s", v.Name, err)
	}
	if len(m["access_token"]) == 0 || len(m["expires"]) == 0 {
		return fmt.Errorf("Values missing from long-lived token response for %s: %s", v.Name, err)
	}
	if err := SetAccessToken(u, m["access_token"][0], m["expires"][0]); err != nil {
		return fmt.Errorf("Could not set long-lived access token for %s: %s", v.Name, err)
	}
	s.Values[userIDSessionKey] = u.ID
	if err := s.Save(r, w); err != nil {
		log.Println(err)
	}
	return HTTPRedirect{To: "/", Code: http.StatusSeeOther}
}
示例#12
0
func (f Facebook) GetUser(tokenValue string) (fb.Result, error) {
	return fb.Get("/me", fb.Params{
		"access_token": tokenValue,
	})
}