将对象发布到Facebook openGraph

时间:2012-04-20 14:07:41

标签: facebook facebook-opengraph

我正在尝试通过opengraph创建自定义对象并将其发布到Facebook时间线。已经在Facebook上关注开发人员的教程。

我遇到了几个错误 - 首先是如果我尝试从教程(使用js SDK)以建议的方式发布对象,我会收到以下错误消息:

OG:(#3502)网址http://markaprice.co.uk/2012dev/fb-ojects/dow.html的对象有og:'网站'类型。属性'travel_goal'需要一个og对象:type'mp-object-ns:travel_goal'

(在元中正确设置了类型)

做了一些研究之后,似乎这可能是由于同时发出的http请求,我读到的一条建议说你可以强制openGraph抓取页面,然后执行你的帖子以避免这个问题 -

我试过了,除了我现在遇到了一个不同的错误:

scrape:(#803)您请求的部分别名不存在:id = http:

我在:

建立了一个非常粗略的工作示例

http://www.markaprice.co.uk/2012dev/fb-objects/dow.html

请有人可以告诉我我做错了什么,因为我不确定接下来要做什么 - 非常感谢你。

我的代码在这里:

// JavaScript Document
// first things first - make call to fb.init with app id to get initial authorisation 
window.fbAsyncInit = function () {
    FB.init({
        appId: 'xxxxxxxxxxxxxx',
        // App ID
        status: true,
        // check login status
        cookie: true,
        // enable cookies to allow the server to access the session
        xfbml: true // parse XFBML
    });
    // call function below
    getSocial()

};

// Load the SDK Asynchronously
(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));


function getSocial() {

    var elements = jQuery('.fb-ac-el-hd');

    // call to fb.getLoginStatus, and pass result tyo handleSessionResponse function 
    FB.getLoginStatus(handleSessionResponse);

    // bind login click to fb.login process, pass response to function, and get required permissions as parameters of fb.login method - the scope method is used to define the permissions sought
    $('#fb-login').bind('click', function () {
        FB.login(function (response) {
            handleSessionResponse(response);
        }, {
            scope: 'user_about_me, user_hometown, user_interests, user_activities, user_likes, user_birthday, user_photos, friends_likes, publish_actions'
        });


    });

    // bind logout to fb.logout, and pass response to handleSessionResponse 
    $('#logout').bind('click', function () {
        FB.logout(handleSessionResponse);

    });

    // disconnect from graphAPI completely, and revoke app authorisation - user needs to be offered the opportunity to 
    $('#fb-disconnect').bind('click', function () {
        FB.api({
            method: 'Auth.revokeAuthorization'
        }, function (response) {
            clearDisplay();
        });
    });




    // handle a session response from any of the auth related calls
    function handleSessionResponse(response) {
        console.log(response);
        // if we have a connect session run the getResponse function
        if (response.status === 'connected') {
            getResponse();
            // else run the clear display function, we no longer have auth to display the users information     
        } else {
            clearDisplay();
            return;
        }

    }

    // diagnostic - see what happens when shutting down machine etc
    FB.Event.subscribe('auth.authResponseChange', function (response) {
        console.log(response); // do something with response
    });



    function clearDisplay() {

        jQuery(elements).fadeOut();

        jQuery('#fb-login').fadeIn();

    }


    function getResponse() {


        // different, more flexible method of connection this time - fql.multiquery
        //can run multiple queries against the graphAPI to return data objects 
        FB.api({
            method: 'fql.multiquery',
            queries: {
                queryA: 'select name, first_name, pic_square, birthday, activities, movies, books, hometown_location FROM user WHERE uid=me()',
                queryB: 'select uid, page_id, type from page_fan WHERE uid=me() and page_id = "xxxxxxxxx"',
                queryC: 'select name, pic_square from user where uid in (select uid from page_fan where page_id ="xxxxxxxxxxx" and uid in (select uid2 from friend where uid1 = me()))'
            }
        }, function (response) {


            //disagnostic only - want to see the responses for now - no need for this in a live environment
            //for (var i = 0; i < response.length; i++) {
            //console.log(response[i]);
            //}

            if (!jQuery(elements).is(':visible')) {

                jQuery(elements).fadeIn();

            }

            // reference the first object returned - response[0].fql_result_set[0]
            var user = response[0].fql_result_set[0];



            var notGiven = "This Information is not provided by this user";

            var fbUname = 'Hello,' + user.first_name;

            jQuery('#fb-name').text(fbUname);

            // grab the users big pic
            var picture = user.pic_square;
            // inject it into the iterface 
            jQuery('.fb-pfl-pic').html('<img class=\"fb-pfl-img\" src="' + picture + '">');




            var likesAvios = response[1].fql_result_set[0];

            /// display appropriate result 
            if (likesAvios) {



            } else {


            }



            // friends list 
            var friendsList = $('#fb-friends-list');

            var noFriends = "At present none of your friends like Euro Miles. Why not recommend Euro Miles to them";

            var friendResults = response[2].fql_result_set;

            if (friendResults.length < 1) {

                friendsList.append(jQuery('<div class=\"fb-nf\">' + noFriends + '</div>'));

            } else {

                if (jQuery('div.frnd-grid-item').length < 1) {

                    for (i = 0; i < friendResults.length; i++) {


                        friendName = friendResults[i].name;
                        friendPic = friendResults[i].pic_square;
                        friendsList.append(jQuery('<div class=\"frnd-grid-item\"><img src=\"' + friendPic + '\"><div class=\"frnd-name\"> ' + friendName + '</div></div>'));

                    }

                }

            }

        }); // close api call

        jQuery('#fb-login').fadeOut();

    } // close function 

    jQuery('#postBtn').click(function () {

        postGoal();

    });



    function postGoal() {
        // set up the variables for the openGrapUrl
        var OG_NAMESPACE = 'mp-object-ns';
        var OG_ACTION = 'set';
        var OG_OBJECT = 'travel_goal';
        var siteURL = 'http://markaprice.co.uk/2012dev/fb-ojects/dow.html';
        var appid = '';

        var openGraphUrl = '/me/' + OG_NAMESPACE + ':' + OG_ACTION + '?' + OG_OBJECT + '=' + siteURL;

        // set up the scrape url
        var scrapeUrl = '/id=' + siteURL + '&scrape=true';


        function postFB() {
            // get the fb api to scrape the url first
            FB.api(scrapeUrl, 'post', function (response) {

                if (!response || response.error) {
                    console.log('scrape: ' + response.error.message);
                } else {
                    // once that is done, post the object out to to the api       
                    FB.api(openGraphUrl, 'post', function (response) {
                        if (!response || response.error) {
                            console.log('OG: ' + response.error.message);
                        } else {
                            console.log('Successful! Action ID: ' + response.id);
                        }
                    });
                }


            });



        } // close postFB
        var fbPost = setTimeout(postFB, 5000);

    } // closePostGoal


} // close getSocial function

0 个答案:

没有答案