criso / fbgraph

NodeJs module to access the facebook graph api
http://criso.github.io/fbgraph/
1.09k stars 176 forks source link

Duplicate message #15

Closed anddimario closed 11 years ago

anddimario commented 11 years ago

Hi, i've this code:

    // Set index route
    app.get('/', utils.accesslog, function(req, res){
        //Get token from session to allow post
        graph.setAccessToken(req.session.fb_token);
        console.log(req.session.fb_token); 
        var wallPost = {
          message: "Hello!"
        };
        // To post you need to authorize the applicationt to public_stream: http://facebook.com/authorize.php?api_key=MYAPPAPIKEY&v=1.0&ext_perm=publish_stream
        graph.post(req.session.fb_userId + "/feed", wallPost, function(err, results) {
          // returns the post id
              if (err){
              console.log(JSON.stringify(err)); // { id: xxxxx}
              }else {console.log(JSON.stringify(results));}
        });
        res.render('index', {connected: req.session.connected, title: configuration.Params.title, message: [{msg: "Hello" + req.session.fb_userId}]});

    });

    // Set facebook login path. On Facebook main parameter page set url as: mysite.tld/fblogin
    app.get('/fblogin', utils.accesslog, function(req, res){
        // we don't have a code yet
        // so we'll redirect to the oauth dialog
        if (!req.query.code) {
            var authUrl = graph.getOauthUrl({
                "client_id":     configuration.Params.facebook_appId,
                "redirect_uri":  configuration.Params.facebook_redirect_uri
            });

            if (!req.query.error) { //checks whether a user denied the app facebook login/permissions
                res.redirect(authUrl);
            } else {  //req.query.error == 'access_denied'
                res.send('access denied');
            }
            return;
        }

        // code is set
        // we'll send that and get the access token
        graph.authorize({
            "client_id":      configuration.Params.facebook_appId,
            "redirect_uri":   configuration.Params.facebook_redirect_uri,
            "client_secret":  configuration.Params.facebook_secret,
            "code":           req.query.code
        }, function (err, facebookRes) {
            console.log(facebookRes); 
            req.session.fb_token = facebookRes.access_token;  
          // Get user information and put them into session
            graph.get("me", function(err, user) {
                req.session.fb_userId = user.id;
                console.log(user); // { id: '4', name: 'Mark Zuckerberg'... }

                res.redirect('/');
            });
        });
    });```

I have two duplicate message to facebook:
{"id":"1657787844_3625402647858"}
{"message":"(#506) Duplicate status message","type":"OAuthException","code":506,"error_data":{"kError":1455006}}
It seems that pages is refreshed twice, probably i wrote something bad.

Had someone the same problem?

Thanks for your time.
Best regards.