Facebook的C#-sdk MVC"的Hello World"应用程序 - 如何获得访问令牌? [英] facebook-C#-sdk MVC "Hello World" app - how to get access token?

查看:262
本文介绍了Facebook的C#-sdk MVC"的Hello World"应用程序 - 如何获得访问令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经下载了C#的Facebook SDK简单MVC网站示例从CodePlex上的:

I've downloaded the C# Facebook SDK "Simple MVC Website Example" from CodePlex at:

http://facebooksdk.codeplex.com/releases/view/54371

和已经成功地得到它来验证我的测试Facebook应用程序。但我不是很制定出如何获得访问令牌(我会想离线访问,所以我只需要抓住该令牌一次,当用户第一次授权我的应用程序来抓住他们的饲料数据)。

and have successfully got it to authenticate to my test Facebook app. But I can't quite work out how to get the access token (and I'm going to want offline access, so I only need to grab that token once, when the user first authorizes my app to grab their feed data).

非常感谢

推荐答案

您会希望做两件事情。首先,要求offline_access,您需要更改的JavaScript登录功能要求离线访问如下。这是在查看/主页/的Index.aspx

You will want to do two things. First, to request offline_access, you need to change the Javascript login function to request offline access as follows. This is in the 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>    



接下来,以获得访问令牌,你只是做在行动以下用户通过验证后:

Next, to get the access token, you just do the following in action after the user is authenticated:

    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();
    }

这篇关于Facebook的C#-sdk MVC&QUOT;的Hello World&QUOT;应用程序 - 如何获得访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆