Facebook FB.ui send dialog intermittently returns invalid link error

二次信任 提交于 2019-11-27 21:30:06

I had the same issue and that stopped working since last week.

Here is my solution:

Go to Facebook debugger and add your URL. Click on "Debug" and fix all warnings displayed by Facebook.

To fix mine I had to add the og.url meta tag in my page. The value should be exactly the same as the one you want to share (no redirection). Then Facebook sent me this notification (alert):

Your app, XXX, is now compliant with the Stream post URL security migration. No further action is required.

Try to share your link with FB.ui once again and now your post should automatically display your og values.

Note: Facebook October breaking change will automatically use those og metas instead of custom FB.ui parameters, therefore you can now activate the breaking change to get ready and remove name/picture/description from your code.

UPDATE: This problem can happens again even if what I mentioned above is correctly implemented.

If it is your case that's because you have to enforce Facebook to "scrape" your page. This process is automatically done by Facebook when you use the Facebook object debugger or you copy/paste your link on your timeline / private messages. If you use the JavaScript SDK you have to ask Facebook to index and cache your page.

You won't find this in the Facebook documentation related to the JavaScript SDK (or you are lucky) so to save you all the days I lost to find this unbelievable issue (remember Facebook only said your link is invalid) you can find more details on this page.

I tried to use the Graph API to enforce my newly created page to be scraped by Facebook, if it works for you you are lucky. The second method which is not mentionned but produced the same result is to send a request to the Facebook Object Debugger page and add your page link in the URL (e.g. https://developers.facebook.com/tools/debug/og/object?q=YourPageUrlHere). By doing this Facebook will this time scrape your page and now you can share your links with the Facebook API, everything is now working.

I can confirm that this was fixed for me when I forced Facebook to first scrape the URL before trying to send that same URL via the FB UI Dialog.

Sample code:

FB.api('https://graph.facebook.com/', 'post', {
    id: '[URL]',
    scrape: true
}, function(response) {
    FB.ui({
        method: 'send',
        name: '[name]',
        picture: '[Picture URL]',
        link: [URL]',
        description: '[description]'
    });
});
hexiecode

Make sure that when facebook requests your url, it will never be forwarded.

A way to do this is to present facebook with a special page of its own, containing all the right ingredients.

Here is an example (sort of based on php + symfony 1.4):

<?php
    if(preg_match('/facebookexternalhit/i', $request->getHttpHeader('User-Agent')))
    { ?>
        <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      <head>
        <meta property="fb:app_id" content="<?php echo sfConfig::get('app_facebook_app_id'); ?>" />
        <meta property="og:url" content="<?php echo sfContext::getInstance()->getRequest()->getUri(); ?>" />
        <meta property="og:title" content="<?php echo $title; ?>" />
        <meta property="og:description" content="<?php echo $description; ?>" />
        <meta property="og:image" content="<?php echo $image; ?>" />
      </head>
      <body>
        hello Facebook!
      </body>
    </html>
    <?php
    }
?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!