0

I am really struggling with the facebook api. A have created a app, an object an action, and I want to test to publish in my stream, (I know the public publishing has to be authorised by facebook but as an adminsitrator of my app, I am allowed to test it). But it doesn't wor. Here's the script :

<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US"
      xmlns:fb="https://www.facebook.com/2008/fbml"> 
    <head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# mehdientest:http://ogp.me/ns/fb/mehdientest#">
        <title>OG Tutorial App</title>
        <meta property="fb:app_id"      content="312683425452812" /> 
        <meta property="og:type"        content="mehdientest:Campain" /> 
        <meta property="og:url"         content="http://www.example.com/pumpkinpie.html" /> 
        <meta property="og:title"       content="Sample Campain" /> 
        <meta property="og:description" content="Some Arbitrary String" /> 
        <meta property="og:image"       content="https://s-static.ak.fbcdn.net/images/devsite/attachment_blank.png" />
    </head>
    <body>
        <div id="fb-root"></div>
        <script>
            window.fbAsyncInit = function() {
                FB.init({
                    appId      : '312683425452812', // App ID
                    status     : true, // check login status
                    cookie     : true, // enable cookies to allow the server to access the session
                    xfbml      : true  // parse XFBML
                });
            };

            (function(d){
                var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
                js = d.createElement('script'); js.id = id; js.async = true;
                js.src = "//connect.facebook.net/en_US/all.js";
                d.getElementsByTagName('head')[0].appendChild(js);
            }(document));

            FB.Event.subscribe('auth.login', function(response) {
                // do something with response
                login();
            });

            FB.Event.subscribe('auth.logout', function(response) {
                // do something with response
                logout();
            });

            FB.getLoginStatus(function(response) {
                if (response.session) {
                    // logged in and connected user, someone you know
                    login();
                }
            });

            function login() {
                FB.login(function(response) {
                    if (response.authResponse) {
                        FB.api('/me', function(response) {
                            alert('Good to see you, ' + response.name + '.');
                        });
                    } else {
                        console.log('User cancelled login or did not fully authorize.');
                    }
                });
            }

            function publish() {
                FB.api('/me/mehdientest:Create?Campain=http://www.example.com/pumpkinpie.html', 'post', function(response) {
                    if (!response || response.error) {
                        alert('msg');
                    }
                });
            }         

            function logout(){ 
                FB.logout(function(response){});
            }
        </script>
        <h3>Example</h3>
        <p>
            <img title="Example" src="" width="550"/>
            <input type="button" value="Logout" onclick="logout();"></input>
            <input type="button" value="Login" onclick="login();"></input>
            <input type="button" value="Publish" onclick="publish();"></input>
            <div class="fb-login-button" data-show-faces="true" data-width="200" data-max-rows="1"></div>
        </p>
    </body>
</html>

I have even tried to call the publish() function in the head area, but this doesn't work also.

Does anybody has an idea ?

Best, Newben

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
epsilones
  • 11,279
  • 21
  • 61
  • 85

1 Answers1

0

Saying "it does not work" is not enough, you should always write what happens so that it will be easier to help you.

In any case, your code was a mess, what's the point of calling FB.login when the user is already logged in?

Here's a modified version of your code, I haven't tested it but it should point you in the right direction. After you play with it a bit, and if it's still not working, please come back and explain what you get.

<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US"
      xmlns:fb="https://www.facebook.com/2008/fbml"> 
    <head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# mehdientest:http://ogp.me/ns/fb/mehdientest#">
        <title>OG Tutorial App</title>
        <meta property="fb:app_id"      content="312683425452812" /> 
        <meta property="og:type"        content="mehdientest:Campain" /> 
        <meta property="og:url"         content="http://www.example.com/pumpkinpie.html" /> 
        <meta property="og:title"       content="Sample Campain" /> 
        <meta property="og:description" content="Some Arbitrary String" /> 
        <meta property="og:image"       content="https://s-static.ak.fbcdn.net/images/devsite/attachment_blank.png" />
    </head>
    <body>
        <div id="fb-root"></div>
        <script>
            window.fbAsyncInit = function() {
                FB.init({
                    appId      : '312683425452812', // App ID
                    status     : true, // check login status
                    cookie     : true, // enable cookies to allow the server to access the session
                    xfbml      : true  // parse XFBML
                });
            };

            (function(d){
                var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
                js = d.createElement('script'); js.id = id; js.async = true;
                js.src = "//connect.facebook.net/en_US/all.js";
                d.getElementsByTagName('head')[0].appendChild(js);
            }(document));

            FB.Event.subscribe('auth.login', function(response) {
                FB.api('/me', function(response2) {
                    alert('Good to see you, ' + response2.name + '.');
                });
            });

            FB.Event.subscribe('auth.logout', function(response) {
                alert("ba bye");
                document.getElementById("publish").disabled = "disabled";
            });

            function loggedin() {
                document.getElementById("publish").disabled = false;
            }

            function publish() {
                FB.api('/me/mehdientest:Create?Campain=http://www.example.com/pumpkinpie.html', 'post', function(response) {
                    if (!response || response.error) {
                        alert('msg');
                    }
                });
            }
        </script>
        <h3>Example</h3>
        <p>
            <img title="Example" src="" width="550"/>
            <input type="button" value="Logout" onclick="logout();"></input>
            <input type="button" value="Login" onclick="login();"></input>
            <input type="button" id="publish" value="Publish" onclick="publish();" disabled="disabled"></input>
            <div class="fb-login-button" data-show-faces="true" data-width="200" data-max-rows="1"></div>
        </p>
    </body>
</html>
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • Hi, Thank you for your quick response. In fact, it still didn't work. I changed the error message in the fb.api call by this : if (!response || response.error) {alert(response.error.message); So here's the message I obtained : "The action you're trying to publish is invalid because it does not specify any reference objects. At least one of the following properties must be specified: campain". But in my app dashboard, I specified an object and an action and I targeted the action to the object. I don't really know what it is going wrong... – epsilones Apr 02 '12 at 12:22
  • Are you sure that the verb (Create) and noun (Campain) you submitted are in caps? Because the error message you posted kinda shows that it expects: campain. Everything in the facebook api is case sensitive. – Nitzan Tomer Apr 02 '12 at 12:28
  • IN fact, I submitted them in caps. But I changed them to lower case. And now, the error message changed to "(#3502) Object at URL http://www.example.com/pumpkinpie.html has og:type of 'website'. The property 'campain' requires an object of og:type 'mehdientest:campain'. " Does it mean anything ? – epsilones Apr 02 '12 at 12:56
  • Yeah, in the open graph tags (which start with "og") you have in that url you probably have "website" as the type, and it should be "mehdientest:campain". The message is pretty straight forward. – Nitzan Tomer Apr 02 '12 at 13:26
  • So sorry, I am really new to fb API. I don't understand because in the meta the og-type seems ok. And in my app's dashboard, the url of the object 'campain' is something like "http://samples.ogp.me/..." Do you mean I can't target a website with this object ? Thank you so much for your response. Best, Newben – epsilones Apr 02 '12 at 13:43
  • Can you please give the actual url you are trying to publish? – Nitzan Tomer Apr 02 '12 at 13:45
  • You haven't understood how to use the open graph. You need to put og tags in your page that will tell facebook what object the page describes. Check it out in the [debugger](http://developers.facebook.com/tools/debug/og/object?q=http%3A%2F%2Fwww.primagora.net%2FFirstscript2.html) Fix the errors . – Nitzan Tomer Apr 02 '12 at 14:03
  • Hi, after checking, it worked. The problem was that in my app dashboard, I attached as a url "primagora.net/Firstscript.html" instead of "primagora.net/Firstscript2.html". Thanks a lot !!! – epsilones Apr 02 '12 at 14:24
  • Ok, good. So can you please accept this answer? Just click the 'v' next to the answer. – Nitzan Tomer Apr 02 '12 at 14:51