Andreas
Andreas

Reputation: 2376

set title, description and url for facebbok feed with javascript

I have this code ->

window.fbAsyncInit = function () {
        FB.init({
            appId: 'AppId',
            xfbml: true,
            version: 'v2.1'
        });
    };
    (function (d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) { return; }
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/en_US/sdk.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));

EDIT: The next code part is being called upon after document is ready and an element is clicked on ->

FB.ui({
                method: 'share_open_graph',
                action_type: 'og.likes',
                action_properties: JSON.stringify({
                    "og:url": url,
                    "og:title": settings.pageName,
                    "og:description": description
                })
            }, function (response) { });

and get an error where it says I have to define an object. All I want to do is to decide myself what to set as title, description, url and image. Without making a lot of configuration in the app-settings page. Is this possible?

As I understand I have to create a story object of some sort but that seems a bit overkill for my siuation.

Thanks!

EDIT: Error message added, english translation after.

Action Requires At Least One Reference: Åtgärden som du försöker publicera är inte giltig eftersom det inte finns något referensobjekt angivet. Åtminstone en av följande egenskaper måste specificeras: object.

Action Requires At Least One Reference: The action you are trying to publish is invalid because there is no reference object set. At least one of the following must be specified: Object.

Error code is 1611072.

Upvotes: 0

Views: 457

Answers (1)

andyrandy
andyrandy

Reputation: 74014

the JavaScript SDK is not initialized when you try to use FB.ui. You need to call FB.ui after FB.init. But i suggest calling FB.ui only directly on user interaction (mouse click), else it may get blocked in the browser.

You have to think asynchronous.

About the parameters: According to the docs, you can´t dynamically set title and description, there is only the URL and i assume it will take the Open Graph data from that URL:

FB.ui({
  method: 'share_open_graph',
  action_type: 'og.likes',
  action_properties: JSON.stringify({
      object:'https://developers.facebook.com/docs/',
  })
}, function(response){});

Source: https://developers.facebook.com/docs/sharing/reference/share-dialog

And yes, you would need to create an "Action Type" in the Developer Settings (Open Graph > Action Types) for that, and you would need to go through a review process with og.likes. So if you just want to share something, this would be the easiest way:

FB.ui({
  method: 'share',
  href: 'https://developers.facebook.com/docs/',
}, function(response){});

But even with that solution, you can´t define custom title/description, it will always take the Open Graph tags.

Upvotes: 1

Related Questions