简体   繁体   English

Facebook不赞成使用离线访问,还是可以吗?

[英]Facebook deprecated offline access, any way around?

I have a music website in php. 我在php中有一个音乐网站。 I can get users to authenticate via facebook and login to my website via hybridauth library. 我可以让用户通过facebook进行身份验证,并通过hybridauth库登录到我的网站。

Now what I want is that every time a user listens to a song I update his status with the song link. 现在我想要的是,每当用户收听一首歌曲时,我都会通过歌曲链接更新其状态。 Can this be achieved now that Facebook is removing the support for offline access or is this something that does not need the offline access permission? Facebook取消了对脱机访问的支持后,现在是否可以实现此目的?或者这是不需要脱机访问权限的东西吗?

I am new to facebook api. 我是Facebook API的新手。

Well, I don't fully understand your scenario, but if the user is using your app at the time you want to publish on his wall then you have no problem what so ever, and you do not need the "offline_access" or any equivalent.. 好吧,我还不太了解您的情况,但是如果用户在您要发布在墙上的时候正在使用您的应用程序,那么您将不会有任何问题,并且不需要“ offline_access”或任何等效项..

You can simply load the javascript SDK to the page, which will make sure that the user is authenticated and that you have a valid access token, as it says in the auth documentation Handling Invalid and Expired Access Tokens : 您可以简单地将javascript SDK加载到页面上,这将确保用户已通过身份验证,并且您具有有效的访问令牌,如auth文档中处理无效和过期的访问令牌所述

Desktop Web and Mobile Web apps which implement authentication with the Javascript SDK 通过Javascript SDK实现身份验证的桌面Web和移动Web应用程序

Calling FB.getLoginStatus() or ensuring status: true is set when you call FB.init() means that the next time a user lands on your application and is signed into Facebook, the authResponse object you are passed as a result of those calls will contain a fresh, valid access token. 调用FB.getLoginStatus()或确保状态:在您调用FB.init()时设置为true意味着用户下次登陆您的应用程序并登录Facebook时,由于这些调用而传递了authResponse对象将包含一个新的有效访问令牌。

In this case, its simply the act of the user using your application which implicitly generates a new access token. 在这种情况下,这只是用户使用您的应用程序的行为,它隐式生成一个新的访问令牌。


Edit 编辑

You can get a pretty long lived access token (about 60 days) if you use the server side auth flow, or using the new endpoint facebook provided for extending valid access tokens (which were obtained using a client-side flow) as described in the post Removal of offline_access Permission . 如果使用服务器端身份验证流,或者使用提供的用于扩展有效访问令牌的新端点facebook(使用客户端流获取),则可以获得较长的访问令牌(约60天)。后删除offline_access权限

If that helps you then fine, but I recommend you to just make sure that your application can handle token expiration since there's nothing you can do to completely avoid that situation. 如果这对您有帮助,那么可以,但我建议您仅确保您的应用程序可以处理令牌到期,因为您无法采取任何措施来完全避免这种情况。

How? 怎么样? well, you have a few options, and you'll need to find one that fits your specific scenario, but here's an option: First of all, as I see it, you'll have to do things in the client side since the token ca expire when the user is listening to a song, and in order to get a new token from the server side you'll have to refresh the page and send the user to the auth dialog, which will interfere with his experience. 好吧,您有几个选择,您需要找到一个适合您特定情况的选择,但这是一个选择:首先,正如我所看到的,自令牌以来,您必须在客户端进行操作当用户正在聆听歌曲时,ca会过期,并且为了从服务器端获取新令牌,您必须刷新页面并将用户发送到auth对话框,这会干扰他的体验。

Here's some js code that uses the js sdk, it's not full code, and I don't expect it to work, but it should give you an idea of what you can do: 这是一些使用js sdk的js代码,它不是完整的代码,我不希望它能正常工作,但是它应该使您对可以做什么有所了解:

FB.Event.subscribe("auth.statusChange", function(response) {
    if (response.status !== "connected") {
        FB.login(function(response2) {
            if (response2.authResponse) {
                // you now have a valid access token, you can update your server side with an ajax request for example
            }
            else {
                // user canceled login
            }
        });
    }
});

Also, be aware that FB.login tries to open a popup and so it should not be called like this, only after a user interaction, and so instead of just calling it, you should replace it with showing a user a message like "please click 'ok' in order to be able to post to facebook..." and with that click open the FB.login . 另外,请注意, FB.login会尝试打开一个弹出窗口,因此不应仅在用户交互后才这样调用它,因此,与其仅调用它,还应替换为向用户显示一条消息,例如“请单击“确定”,以便能够发布到Facebook ...”,然后单击打开FB.login

There are of course other methods that you can use, or other events that you can subscribe to, just read the documentation and experiment with the options you have. 当然,您可以使用其他方法或可以订阅的其他事件,只需阅读文档并尝试使用所拥有的选项即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM