Facebook Callback将"#_ = _"附加到返回URL

zin*_*ing 468 facebook returnurl

Facebook回调已经开始#_=_在返回URL中添加哈希下划线

有谁知道为什么?解决办法是什么?

Rya*_*yan 234

通过Facebook的平台更新:

会话重定向行为的更改

本周,当此字段留空时,我们开始向redirect_uri添加片段#____ = ____.请确保您的应用可以处理此行为.

要防止这种情况,请在登录URL请求中设置redirect_uri,如下所示:(使用Facebook php-sdk)

$facebook->getLoginUrl(array('redirect_uri' => $_SERVER['SCRIPT_URI'],'scope' => 'user_about_me'));
Run Code Online (Sandbox Code Playgroud)

UPDATE

以上内容正如文档中所说的那样.但是,Facebook的文档化解决方案不起作用.请考虑在Facebook平台更新博客文章上发表评论并关注此错误以获得更好的答案.在此之前,请在head标记中添加以下内容以解决此问题:

<script type="text/javascript">
    if (window.location.hash && window.location.hash == '#_=_') {
        window.location.hash = '';
    }
</script>
Run Code Online (Sandbox Code Playgroud)

或者更详细的替代方案(感谢niftylettuce):

<script type="text/javascript">
    if (window.location.hash && window.location.hash == '#_=_') {
        if (window.history && history.pushState) {
            window.history.pushState("", document.title, window.location.pathname);
        } else {
            // Prevent scrolling by storing the page's current scroll offset
            var scroll = {
                top: document.body.scrollTop,
                left: document.body.scrollLeft
            };
            window.location.hash = '';
            // Restore the scroll offset, should be flicker free
            document.body.scrollTop = scroll.top;
            document.body.scrollLeft = scroll.left;
        }
    }
</script>
Run Code Online (Sandbox Code Playgroud)

  • 荒谬.他们甚至无法获得自己的文档 (72认同)
  • @Ryan Update几乎对我有用,我最后还是得到一个哈希(/#).对FB不满意. (11认同)
  • 什么字段留空?这非常神秘 (10认同)
  • 此解决方案将擦除散列:<script type ="text/javascript"> var idx = window.location.toString().indexOf("#_ = _"); if(idx> 0){window.location = window.location.toString().substring(0,idx); } </ script>确保这是head元素中的第一个标记. (5认同)
  • 我仍然得到/#.有谁在这里更新?将#删除 (2认同)

Pau*_*arz 112

TL; DR

if (window.location.hash === "#_=_"){
    history.replaceState 
        ? history.replaceState(null, null, window.location.href.split("#")[0])
        : window.location.hash = "";
}
Run Code Online (Sandbox Code Playgroud)

完整版与分步说明

// Test for the ugliness.
if (window.location.hash === "#_=_"){

    // Check if the browser supports history.replaceState.
    if (history.replaceState) {

        // Keep the exact URL up to the hash.
        var cleanHref = window.location.href.split("#")[0];

        // Replace the URL in the address bar without messing with the back button.
        history.replaceState(null, null, cleanHref);

    } else {

        // Well, you're on an old browser, we can get rid of the _=_ but not the #.
        window.location.hash = "";

    }

}
Run Code Online (Sandbox Code Playgroud)

一步步:

  1. 如果fragment是,我们只会进入代码块#_=_.
  2. 检查浏览器是否支持HTML5 window.replaceState方法.
    1. 通过拆分#并仅采用第一部分来清理URL .
    2. 告诉history用干净的URL替换当前页面状态.这会修改当前历史记录条目,而不是创建新条目.这意味着后退和前进按钮将按照您想要的方式工作.;-)
  3. 如果浏览器不支持令人敬畏的HTML 5历史记录方法,那么只需将哈希设置为空字符串即可尽可能地清理URL.这是一个糟糕的后备,因为它仍然留下一个尾随哈希值(example.com/#),并且它还添加了一个历史记录条目,因此后退按钮会将您带回#_-_.

了解更多信息history.replaceState.

了解更多信息window.location.

  • 对我来说也很完美.另一个解决方案摆脱了任何查询参数. (2认同)

lik*_*ats 59

如果你想从网址中删除剩余的"#"

$(window).on('load', function(e){
  if (window.location.hash == '#_=_') {
    window.location.hash = ''; // for older browsers, leaves a # behind
    history.pushState('', document.title, window.location.pathname); // nice and clean
    e.preventDefault(); // no page reload
  }
})
Run Code Online (Sandbox Code Playgroud)

  • $(window).on('load',function(e){/*likebeats的代码*/}有效. (6认同)
  • 试过这个.控制台中出现'e is not defined'错误. (2认同)

Mar*_*phy 43

出于安全原因,这是由Facebook设计实现的.以下是Facebook团队成员Eric Osgood的解释:

这被标记为"按设计",因为它可以防止潜在的安全漏洞.

某些浏览器会将URL中的哈希片段附加到已重定向到的新URL的末尾(如果新URL本身没有哈希片段).

例如,如果exam​​ple1.com返回到example2.com的重定向,那么访问example1.com#abc的浏览器将转到example2.com#abc,而example1.com上的散列片段内容可以访问example2上的脚本.COM.

由于可以将一个auth流重定向到另一个,因此可以从一个应用程序访问另一个应用程序的敏感身份验证数据.

通过将新的哈希片段附加到重定向URL来缓解此问题,以防止此浏览器行为.

如果关注结果URL的美学或客户端行为,则可以使用window.location.hash(甚至是您自己的服务器端重定向)来删除有问题的字符.

资料来源:https://developers.facebook.com/bugs/318390728250352/

  • 这是唯一能够解释为什么会发生这种情况的答案,谢谢,我想我会在我的网址中留下令人讨厌的字符,因为我知道它们不是问题. (6认同)

mix*_*lan 10

不知道为什么他们这样做但是,你可以通过重置页面顶部的哈希来解决这个问题:

if (window.location.hash == "#_=_")
  window.location.hash = "";
Run Code Online (Sandbox Code Playgroud)


pki*_*die 9

您还可以redirect_uri在Facebook回调的参数上指定自己的哈希值,这在某些情况下可能会有所帮助,例如/api/account/callback#home.当你被重定向回来时,如果你使用的是backbone.js或类似的(不确定jquery mobile),它至少会是一个与已知路由相对应的哈希值.


Sán*_*óth 8

Facebook使用一个框架,在其中使用AJAX通信的所有功能.这种情况下最大的问题是保留当前页面状态.据我所知,Facebook决定使用模拟锚点.这意味着如果您在某处单击,它们会将其模拟为页面内的锚点,并且当AJAX通信开始时,它们也会更改URL的锚位.

当您尝试重新加载页面时,此解决方案通常会帮助您(不是按ENTER,按F5),因为您的浏览器会将带有锚点的整个URL发送到Facebook服务器.因此,Facebook会选择最新状态(你所看到的),然后你可以继续从那里开始.

当回调返回时#_=_,表示页面在离开之前处于基本状态.因为浏览器会解析此锚点,所以您不必担心它.

  • 如果你有一个像骨干或者ember这样的javascript框架,这是一个问题,因为路由器会解释哈希之后的所有内容 (2认同)

小智 8

主要烦人,特别是对于解析URI的应用程序,而不只是阅读$ _GET ...这是我扔在一起的黑客...享受!

<html xmlns:fb='http://www.facebook.com/2008/fbml'>
<head>
        <script type="text/javascript">
        // Get rid of the Facebook residue hash in the URI
        // Must be done in JS cuz hash only exists client-side
        // IE and Chrome version of the hack
        if (String(window.location.hash).substring(0,1) == "#") {
                window.location.hash = "";
                window.location.href=window.location.href.slice(0, -1);
                }
        // Firefox version of the hack
        if (String(location.hash).substring(0,1) == "#") {
                location.hash = "";
                location.href=location.href.substring(0,location.href.length-3);
                }
        </script>
</head>
<body>
URI should be clean
</body>
</html>
Run Code Online (Sandbox Code Playgroud)


nee*_*mzy 6

如果你使用带有hashbang(/#!/)URL的JS框架,例如Angular,这可能会成为一个严重的问题.实际上,Angular会将带有非hashbang片段的URL视为无效并抛出错误:

Error: Invalid url "http://example.com/#_=_", missing hash prefix "#!".
Run Code Online (Sandbox Code Playgroud)

如果您遇到这种情况(并重定向到您的域根目录),而不是:

window.location.hash = ''; // goes to /#, which is no better
Run Code Online (Sandbox Code Playgroud)

简单地说:

window.location.hash = '!'; // goes to /#!, which allows Angular to take care of the rest
Run Code Online (Sandbox Code Playgroud)


Seb*_*usk 5

我不知道这个问题是如何与facebook AJAX相关的.实际上,在禁用JavaScript和纯粹基于重定向的登录时也会出现此问题.

与facebook交换的示例:

1. GET <https://www.facebook.com/dialog/oauth?client_id=MY_APP_ID&scope=email&redirect_uri=MY_REDIRECT_URL> RESPONSE 302 Found Location: <https://www.facebook.com/connect/uiserver.php?[...]>  
2. GET <https://www.facebook.com/connect/uiserver.php?[...]> RESPONSE 302 Found MY_REDIRECT_URL?code=FB_CODE#_  
3. GET MY_REDIRECT_URL?code=FB_CODE#_  
Run Code Online (Sandbox Code Playgroud)

仅适用于我的Firefox.