簡體   English   中英

JS SDK的Facebook API發布操作

[英]Facebook API Publishing action with JS SDK

我真的在努力與Facebook API。 一個已經創建了一個應用程序,一個對象一個動作,並且我想測試要在我的流中發布(我知道公開發布必須得到facebook的授權,但是作為我的應用程序的管理員,我可以對其進行測試)。 但這並不擔心。 這是腳本:

<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>

我什至嘗試在頂部區域調用publish()函數,但這也不起作用。

有人有想法嗎?

最好,紐本

僅僅說“不起作用”是不夠的,您應該始終記錄會發生什么,以便更輕松地為您提供幫助。

無論如何,您的代碼都是一團糟,當用戶已經登錄時調用FB.login有什么意義?

這是您代碼的修改后的版本,我尚未對其進行測試,但它可以為您指明正確的方向。 稍作嘗試后,如果仍然無法使用,請返回並解釋一下。

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM