如何在本地实施Facebook登录我的网站?

问题描述 投票:0回答:3

我想在本地实现Facebook登录(连接)到我的网站。我查看了文档,但我自己无法实现。我已经在Facebook上创建了一个App ID。上传网站时应该更改什么?

我试过这个例子:

 <html>
<head>
  <title>My Facebook Login Page</title>
</head>
<body>
  <div id="fb-root"></div>
  <script>
    window.fbAsyncInit = function() {
      FB.init({
        appId      : 'YOUR_APP_ID',
        status     : true, 
        cookie     : true,
        xfbml      : true,
        oauth      : true,
      });
    };
    (function(d){
       var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
       js = d.createElement('script'); js.id = id; js.async = true;
       js.src = "//connect.facebook.net/en_US/all.js";
       d.getElementsByTagName('head')[0].appendChild(js);
     }(document));
  </script>
  <div class="fb-login-button">Login with Facebook</div>
</body>

我收到此错误:

API Error Code: 191 API Error Description: The specified URL is not owned by the application Error Message: Invalid redirect_uri: Given URL is not allowed by the Application configuration.

我把http://localhost:8080/作为我的网站网址。 The documentation says I can use this URL when developing locally

注意:我正在使用Apache,PHP和javascript进行开发。

facebook
3个回答
0
投票

这是一个简单的例子。在创建应用程序时,请确保从您在developers.facebook.com上为您的应用程序配置的域中运行此命令!

<!doctype html>
<html>
<head>
     <script src="//connect.facebook.net/en_US/all.js"></script>
     <script>

        window.fbAsyncInit = function()
        {
            FB.init
            (
                {
                    appId   : "<<YOUR APP ID!>>",
                    status  : true,
                    cookie  : true,
                    oauth   : true
                }
            );
        };

        function login()
        {
            FB.login
            (
                function( response )
                {
                    if ( response.authResponse )
                    {
                        FB.api
                        (
                            "/me",
                            function( response )
                            {
                                document.getElementById( "profile_name" ).innerHTML = response.name;
                                document.getElementById( "profile_pic" ).src = "http://graph.facebook.com/" + response.id + "/picture";
                            }
                        )
                    }
                }
            );
        }

     </script>
</head>
<body>
    <div id="fb-root"></div>
    <a href="javascript:login();">Login</a>
    <img id="profile_pic"/>
    <div id="profile_name"></div>
</body>


0
投票

您是否添加了所需的域以验证API?如果要在本地服务器上实现它,则必须添加本地地址(这是我们在创建画布应用程序时所执行的操作)。

这真的值得阅读the detailed documentation:你可以实现这个例子并从中学习。


0
投票

找出你的系统的id地址,而不是本地主机使用你的IP地址给你使用登录的页面的完整网址Ex http://192.168.0.10:8080/fb.html

© www.soinside.com 2019 - 2024. All rights reserved.