在您的FB应用程序准备好后,的确需要获取令牌。但是,令牌总是在用户的上下文中请求 - 因此用户应该在网站上进行认证。这意味着应该显示Facebook登录对话框 - 这将询问用户他是否确实信任该应用程序。有不同的方式来做到这一点 - 这里是关于FB认证的通用页面:https://developers.facebook.com/docs/authentication/,这个解释了如何在Android中执行此操作:https://developers.facebook.com/docs/mobile/android/sso/
如果您想要使用JavaScript身份验证(我个人在MVC3中使用项目),这里是一个从https://developers.facebook.com/docs/authentication/client-side/这是做了一点点修改的例子。请记住将应用程序ID和权限更改为您需要的。
<div id="fb-root"></div>
<script>
// Load the SDK Asynchronously
(function (d) {
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.js";
ref.parentNode.insertBefore(js, ref);
} (document));
// Init the SDK upon load
window.fbAsyncInit = function() {
FB.init({
appId: '@EventController.FB_APP_ID', // App ID
channelUrl: '//' + window.location.hostname + '/channel.htm', // Path to your Channel File
status: false, // check login status
cookie: false, // enable cookies to allow the server to access the session
xfbml: false // parse XFBML
});
// listen for and handle auth.statusChange events
FB.Event.subscribe('auth.statusChange', function (response) {
if (response.authResponse) {
// user has auth'd your app and is logged into Facebook
FB.api('/me', function (me) {
// get info about the user, for example
})
//document.getElementById('auth-loggedout').style.display = 'none';
} else {
// user has not auth'd your app, or is not logged into Facebook
//document.getElementById('auth-loggedout').style.display = 'block';
}
});
// respond to clicks on the login and logout links
document.getElementById('auth-loginlink').addEventListener('click', function() {
FB.login(function (response) {
// handle the response
if (response.authResponse != null) {
// HERE IS THE TOKEN YOU CAN USE
var token = response.authResponse.accessToken;
} else {
}
// AND THESE ARE THE PERMISSIONS YOU WANT YOUR APP TO REQUEST
}, { scope: 'manage_pages,publish_stream' });
});
};
</script>
一个catch这里 - FB应用程序应该有你的网站URL(即使是http://localhost/)在应用程序 - >设置 - >网站URL字段。你也应该把channel.htm文件到您的网站的根在它的一行代码:
<script src="//connect.facebook.net/en_US/all.js">
我是链接的作者教程和我已经重新访问过这个。用户不需要复制和粘贴任何访问令牌,它是完全自动的。检查出来:http://www.ifc0nfig.com/accessing-facebook-unity-game-2/ – eth0 2013-08-11 00:22:21
@ eth0感谢您的更新! – 2013-08-12 02:12:01
感谢Gunnar的建议,我发现这个插件是最好的https://www.assetstore.unity3d.com/en/#!/content/16676 – Mastro 2015-01-04 01:54:33