Dav*_*e B 9 c# model-view-controller facebook facebook-c#-sdk
我从CodePlex下载了C#Facebook SDK"Simple MVC Website Example":
http://facebooksdk.codeplex.com/releases/view/54371
并已成功通过我的测试Facebook应用程序进行身份验证.但我无法弄清楚如何获取访问令牌(我想要离线访问,所以我只需要抓取一次令牌,当用户首次授权我的应用程序获取他们的Feed数据时).
非常感谢
你会想要做两件事.首先,要请求offline_access,您需要更改Javascript登录功能以请求离线访问,如下所示.这是在Views/Home/Index.aspx中.
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({ appId: '<%:FacebookSettings.Current.AppId %>', status: true, cookie: true, xfbml: true });
$('#fbLogin').click(function() {
FB.login(function (response) {
if (response.session) {
window.location = '<%:Url.Action("Profile") %>'
} else {
// user cancelled login
}
}, { perms: 'offline_access' });
});
</script>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)
接下来,要获取访问令牌,您只需在用户通过身份验证后执行以下操作:
public ActionResult Profile()
{
var app = new FacebookApp();
if (app.Session == null)
{
// The user isnt logged in to Facebook
// send them to the home page
return RedirectToAction("Index");
}
// Read current access token:
var accessToken = app.Session.AccessToken;
// Get the user info from the Graph API
dynamic me = app.Api("/me");
ViewData["FirstName"] = me.first_name;
ViewData["LastName"] = me.last_name;
return View();
}
Run Code Online (Sandbox Code Playgroud)
Hom*_*mde -3
我的建议是您使用新的图形 api,您可以在此处获取它
http://github.com/facebook/csharp-sdk
以下是有关如何获取身份验证令牌的说明:
http://developers.facebook.com/docs/authentication/
| 归档时间: |
|
| 查看次数: |
11336 次 |
| 最近记录: |