【问题标题】:Facebook ignoring part of my query string in share URLFacebook 忽略了我在共享 URL 中的部分查询字符串
【发布时间】:2013-10-06 16:15:06
【问题描述】:

我有一个带有 Facebook 分享按钮的页面。我要共享的 URL 上有一个我用 javascript 构建的查询字符串。这是我生成要共享的 URL 的方式..

queryString = "cup=blue&bowl=red&spoon=green"; 
//the values of this are actually generated by user input, don't think its important for this example though. So in this example its just a basic string.

siteURL = "http://example.com/?share=1&";
//the url without the query string

sharingURL = siteURL+queryString; 
//Combing the domain/host with query string.. sharingURL should = http://example.com?share=1&cup=blue&bowl=red&spoon=green


function FBshare(){
  shareURL = siteURL+queryString;
  console.log(shareURL);
  window.open(
     'https://www.facebook.com/sharer/sharer.php?u='+shareURL, 
     'facebook-share-dialog', 
     'width=626,height=436'); 
  return false;
}


$(".facebook").bind("click", function(){
   FBshare();
});

当 facebook 出于某种原因获取 URL 时,它会忽略在 queryString 变量中创建的所有内容。所以共享的 URL 最终只是 http://example.com/?share=1。有什么想法为什么要放弃 queryString 变量?正确的 URL 被放入 console.log 就好了,加上它在 Facebook share.php URL 中作为查询字符串(例如 https://www.facebook.com/sharer/sharer.php?u=http://example.com/?share=1&cup=blue&bowl=red&spoon=green).. 但 Facebook 上的实际链接不完整。

这是一个 jsFiddle。 http://jsfiddle.net/dmcgrew/gawrv/

【问题讨论】:

    标签: javascript jquery facebook facebook-sharer


    【解决方案1】:

    我在这个特定场景中遇到了这个问题:

    我将分享以下链接:

    FB.ui({
      method: 'share',
      href: 'mysite.com?p=10&og=15',
    }, function(response){});
    

    Facebook 抓取工具会从 URL 中删除 og 参数并抓取以下 URL:mysite.com?p=10

    原因

    1. 我要求 Facebook 刮板刮掉这个链接:mysite.com?p=10&og=15。抓取工具会向给定的 URL 发出请求并获得如下响应:
    <head>
        <meta property="og:url" content="mysite.com?p=10">
        <meta property="og:image" content="mysite.com?photo=1145">
        // more meta tags...
    </head>
    
    1. Facebook 抓取工具抓取我在 og:url 元标记中证明的任何值,并向该 URL 发出请求并抓取该页面。

    解决方案

    我必须修复 og:url 元标记的值以包含所有必需的参数。

    【讨论】:

      【解决方案2】:

      自 2014 年 10 月起,Facebook 已弃用 sharer.php 以及 Facebook Feed 和 Share 对话框。

      目前对链接共享的建议是使用共享对话框:

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

      【讨论】:

        【解决方案3】:

        facebook URL 是这样的:

        https://www.facebook.com/sharer/sharer.php?u=http://example.com?share=1&cup=blue&bowl=red&spoon=green
        

        第一个&amp; 和参数cup(以及其他参数)被解释为facebook url 的一部分。

        使用encodeURIComponent(),它将对&amp;等特殊字符进行编码:

        shareURL = encodeURIComponent(siteURL+queryString);
        

        【讨论】:

          【解决方案4】:

          除了 Jason P 的回答之外,sharer.php 早已被弃用。

          您应该使用 Facebook 动态和分享对话框:https://developers.facebook.com/docs/reference/dialogs/feed/

          这些提供了对共享对话框的更多控制,以及通过Facebook Debugger 进行更好的调试。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-03-04
            • 2011-04-20
            • 2011-09-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-06-02
            • 2014-12-15
            相关资源
            最近更新 更多