gpt4 book ai didi

php - 如何正确使用facebook OAuth2?

转载 作者:行者123 更新时间:2023-12-02 18:55:09 25 4
gpt4 key购买 nike

经过三天的拼命尝试将 Facebook 登录系统集成到我的网站中,我得出的结论是我惨败了。我只是用 JavaScript SDK API 调用创建了一堆 PHP SDK,结果正如预期的那样,不起作用。

我在 Google 和 stackoverflow 上搜索过其他人的问题、教程、解释的答案,但我现在承认自己失败了。

你能给我一份关于这份工作的好的教程吗?或者你能给我深入了解一下他们的图书馆吗?

希望这不会被关闭。

编辑:这是使用 Facebook 登录系统的推荐方法吗?

编辑2:

FB.init({
appId : '<?php echo $core->appId; ?>', // App ID from the App Dashboard
channelUrl : '//romeo.no-ip.org/94seconds/channel.html', // Channel File for x-domain communication
status : true, // check the login status upon init?
cookie : true, // set sessions cookies to allow your server to access the session?
xfbml : true // parse XFBML tags on this page?
});

此代码生成 FB.getLoginStatus() called before calling FB.init().

我的页面如下所示:

<!DOCTYPE html>
<html>
<head>
<script src="../node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $core->appId; ?>', // App ID from the App Dashboard
channelUrl : '//romeo.no-ip.org/94seconds/channel.html', // Channel File for x-domain communication
status : true, // check the login status upon init?
cookie : true, // set sessions cookies to allow your server to access the session?
xfbml : true // parse XFBML tags on this page?
});
};

// Load the SDK's source Asynchronously
// Note that the debug version is being actively developed and might
// contain some type checks that are overly strict.
// Please report such bugs using the bugs tool.
(function(d, debug){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
ref.parentNode.insertBefore(js, ref);
}(document, /*debug*/ false));
</script>
</body>
</html>

编辑 3:没关系,我刚刚意识到我花了 3 - 4 个小时在墙上打洞,因为我放错了一些代码。我已经丢失了错误。 :D 谢谢你们。

最佳答案

给你,兄弟!

  1. 这会将 fb js sdk 添加到您的页面

    (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/all.js#xfbml=1&appId=" + APP_ID;
    fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
  2. 这将通知您用户是否已登录或他当前的状态

    window.fbAsyncInit = function() {

    // Additional init code here
    FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
    // logged in and connected

    } else if (response.status === 'not_authorized') {
    // not_authorized - the user has not authorized ur app

    } else {
    // User is not_logged_in

    }
    });

    FB.Event.subscribe('edge.create',
    function(response) {
    //Not relevant here
    //User has just liked your page
    //likeHandler(response);
    });
    };
  3. 这是为了让用户登录

    function Login() {
    FB.login(function (e) {
    // console.log(e.authResponse);
    if (e.authResponse) {
    M_access_token = e.authResponse.accessToken;

    //custom function to get user details
    get_details();
    } else {
    // console.log("Error : Failed to Authorize")
    }
    }, {
    scope: "email,user_likes,publish_stream"
    })
    }

    //Notice the scope param above
  4. 这用于注销用户

    function Logout() {
    FB.logout(function(response) {
    // user is now logged out
    });
    }
  5. 自定义函数Get_details

    function get_details(typeCalled) {
    FB.api("/me?fields=name,id,email,gender,username,picture", function (e) {
    fbUserObject = e;
    // M_user_id = e.id;
    // M_user_full_name = e.name;
    // M_user_email = e.email;
    // M_user_gender = e.gender;
    // M_username = e.username;
    //picture = e.picture.data.url;
    });
    }
  6. 获得用户数据后,您可以将其传递到服务器并设置 session 。一旦用户单击注销按钮,您应该清除服务器上的 session 。

祝一切顺利! :)

关于php - 如何正确使用facebook OAuth2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15449860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com