11

I'm using FB.ui to share a page to Facebook and I'm trying to set the title and message (image if possible but not important). I have this in my site header

<meta property="og:title" content="Your title here" />
<meta property="og:description" content="your description here" />

And my javascript code is

FB.ui({
      method: 'share',
      href: document.URL,
    }, function(response){

        //TODO Proper response handling
        log(response); 
        if (typeof response != 'undefined') {
            alert('Thanks for sharing');
        }
    }); 

From what I've read I just need to og:titleand og:description to set the title and message, but that doesn't seem to work.

Current the title is either coming from part of the part title, or an alt tag on an image, and the message is being populated from just a random paragraph tag.

TMH
  • 6,096
  • 7
  • 51
  • 88

6 Answers6

35

Facebook documentation says that "share" method has only href parameter, but I have found it is not true. You can use very similar parameters to the "feed" method. This is what I have used and works:

    FB.ui(
    {
        method: 'share',
        href: 'your_url',     // The same than link in feed method
        title: 'your_title',  // The same than name in feed method
        picture: 'path_to_your_picture',  
        caption: 'your_caption',  
        description: 'your_description',
     },
     function(response){
        // your code to manage the response
     });
Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
Janbalik
  • 574
  • 6
  • 15
  • This should be written in the document. Thank you. – Stanley Wang Jun 27 '16 at 06:48
  • 8
    BTW this no longer works as of Graph API v2.9. Its deprecated for the Feed Dialog as well and will officially be dead July 17, 2017. Thanks facebook... /s – Stephen Tetreault May 11 '17 at 19:20
  • 1
    This procedure was vary helpful. So, Is there any solution to pass href , picture, title and description using javascript code? – Mazba Uddin Jul 20 '17 at 06:55
  • Is there any other way to share feed on facebook, I was using v2.7 and it was working fine. But now it suddenly stops sharing content on facebook. Need Help... – Rakhi Prajapati Jul 26 '17 at 07:17
  • I found a workaround, which works as of 20180101 and uses the `share_open_graph` method. See my answer below. – OxC0FFEE Jan 01 '18 at 22:16
  • Nowadays, Facebook has deprecated all properties mentioned above. Any solution to override OG properties with FB.UI method? https://developers.facebook.com/docs/sharing/reference/feed-dialog/ – Shriram Aug 02 '23 at 17:47
19

The code you are using is deprecated. You can use the following share dialogue with dynamically overridden attributes:

FB.ui({
  method: 'share_open_graph',
  action_type: 'og.shares',
  display: 'popup',
  action_properties: JSON.stringify({
    object: {
      'og:url': 'https://your-url-here.com',
      'og:title': 'Title to show',
      'og:description': 'The description',
      'og:image': 'https://path-to-image.png'
    }
  })
}, function(response) {
  // Action after response
});

For a detailed working example, checkout: http://drib.tech/programming/dynamically-change-facebook-open-graph-meta-data-javascript.

If you share a web page (having og meta tags) on Facebook and update the title and description etc later, they will not be get updated instantly on Facebook as it caches your web page and scrap the page again after 2 days.

So if you want to update the title, description etc on Facebook instantly, you'll need to Scrap the web page again using Facebook debug tool.

Faisal Raza
  • 1,337
  • 1
  • 10
  • 16
13

This works for me as of 2018-01-01, using the share-open-graph method. This works, but seems to be magic, and undocumented, so caveat coder.

shareOnFB: function() {
    var img = "image.jpg";
    var desc = "your caption here";
    var title = 'your title here';
    var link = 'https://your.link.here/';

    // Open FB share popup
    FB.ui({
        method: 'share_open_graph',
        action_type: 'og.shares',
        action_properties: JSON.stringify({
            object: {
                'og:url': link,
                'og:title': title,
                'og:description': desc,
                'og:image': img
            }
        })
    },
    function (response) {
        // Action after response
    });
OxC0FFEE
  • 272
  • 3
  • 11
  • 1
    This work perfectly for me. Thank a lot that you save my day! :) – Ye Win Jan 05 '18 at 09:03
  • 1
    This was perfect until a few days ago :(... ref: https://stackoverflow.com/questions/55870767/no-more-custom-parameters-image-title-description-for-fb-ui – Akber Iqbal May 29 '19 at 07:47
7

The meta data might be cached by Facebook. Try entering your url in the Facebook debugger: https://developers.facebook.com/tools/debug/

This will clear the cache.

For image use this:

<meta property="og:image" content="http://yourimage">

Facebook recommends using images with a min size of 1200x630 pixels

intCoffee
  • 184
  • 2
  • 4
2

I found this post and tried to implement the steps mentioned above. After wasting a few hours I've seen the comment from @SMT above...

I definitely doesn't work any more in v2.10.

My customer was already waiting for this feature so I had to find a workaround. Please note: I wrote this solution for WordPress, so you may change a few lines to make it work on your site.

Let's start with my HTML code a wrapper containing the image and the button:

<div class="my-image-container">
    <img src="http://example.com/image.jpg">
    <a href="#" class="fb-share-image">Share</a>');
</div>

In my JS code I add the image url as a parameter to the URL I want to share:

window.fbAsyncInit = function() {
    FB.init({
        appId            : 'YOUR APP ID',
        status           : true,
        cookie           : true,
        version          : 'v2.10'                
    });

    $( '.fb-share-image' ).click(function(e){
        e.preventDefault();
        var image = $(this).siblings('img').attr('src');

        FB.ui(
                {
                    method: 'share',
                    href: $(location).attr('href') + '?og_img=' + image,
                },
                function (response) {

                }
            );
    })
};

(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'));

The next step is to handle the URL parameter. This code is for WordPress and WordPress SEO by YOAST but you can simply change it to work with your CMS. Add this to your functions.php:

add_filter('wpseo_opengraph_image',function( $img ){
    if( array_key_exists( 'og_img', $_GET ) )
        return $_GET['og_img'];
    return $img;
});

add_filter('wpseo_opengraph_url',function( $url ){
    if( array_key_exists( 'og_img', $_GET ) )
        return $url . '?og_img=' . $_GET['og_img'];
    return $url;
});

The general idea is to create an individual URL for each image that only changes the OG parameters so Facebook has to scrape each of them individually. To avoid any SEO issues you should have a canonical tag in your header pointing to the original URL. Here's the complete article.

Hannes
  • 135
  • 3
  • Looks like the workaround I have been searching for as the custom image parameter has lost its functionality. But when I do everything as described clicking my share button opens a message window with "Invalid App ID: 0". Is it that I completely misunderstood what this solution is? I tried to implement it on a regular Wordpress generated page. Have no idea what a Facebook App is etc. – Garavani Sep 04 '17 at 05:54
  • 1
    You need to create your own Facebook App and insert your APP ID on line 3. It only takes a few minutes: https://developers.facebook.com/docs/apps/register/ – Hannes Sep 06 '17 at 05:49
  • Thanks! Done all of that and adapted your scripts. Still can get only the standard Share Window contents (same as from site og). Are you sure that individual images can be achieved for different share buttons on the same page? – Garavani Sep 07 '17 at 07:48
  • I actually got this to work! So, you are my hero! This answer should be linked to all posts beyond July 2017 as a workaround. Great! – Garavani Sep 07 '17 at 08:15
0

If you have public bucket and still you are unable to share your image with facebook then check your backend s3 bucket image upload code.

var data = {
    Bucket: bucketName,
    Key: fileName,
    Body: buf,
    ContentEncoding: 'base64',
    ContentType: 'image/jpeg',

};
s3Bucket.putObject(data, function(err, data){
    if (err) {
        console.log(err);
        console.log('Error uploading data: ', data);
        callback(err);
    } else {
        console.log('succesfully uploaded the image!');
        callback(null,"");
    }
});

Remove ContentType: 'image/jpeg', from data Object.

radhey shyam
  • 759
  • 6
  • 9