Пример #1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        FacebookConfig fbConfig = new FacebookConfig()
        {
            appId = "160789593985275",
            apiSecret = "<secret>"
        };
        //for local debugging and custom persistent store - force domain - should allow cross-domain authentication using a single FB app
        //copy your authtoken from your published app in your local app.
        //        fbConfig.appDomain = "fbAsp.hylas.be";
        /*
        note: for quick and easy debugging on localhost:
        1. create facebook app for local debugging:
        - App Domains: localhost
        - Sandbox Mode: enabled (only admins can use app)
        - Website with Facebook Login: http://localhost:<VS debugger or IIS express port>/Example/
        2. it works! (after a minute or two...)
        3. but you should put appId & apiSecrit in your web.config, using a web.config transformation to switch between your debug & release application
        */
        fb = new Facebook(fbConfig);
        this.initPermissions();
        pnlPermissions.Visible = false;
        if (IsPostBack)
        {
            if (txtAccessToken.Text.Length > 0) fb.setAccessToken(txtAccessToken.Text);
            try
            {
                //fb.getUser should only fail when graph is unavailable or respons is corrupt
                user = fb.getUser();
            }
            catch (Exception ex)
            {
                iPanel.Visible = false;
                lblStatus.Text = "Error communicating with graph servers";
                fbURL.NavigateUrl = fb.getLoginUrl();
                ViewState["user"] = null;
                return;
            }
        }
        else
        {
            try
            {
                user = fb.getUser();
            }
            catch (Exception ex) {
                iPanel.Visible = false;
                lblStatus.Text = "Error communicating with graph servers";
                fbURL.NavigateUrl = fb.getLoginUrl();
                ViewState["user"] = null;
                return;
            }
            if (!string.IsNullOrEmpty(user))
            {
                try
                {
                    iPanel.Visible = true;
                    txtAccessToken.Text = fb.getAccessToken();
                    fbr = fb._graph("/me");
                    txtRequests.Text = "/me";
                    txtData.Text = fbr.ToString();
                    //txtUserObect.Text = fbr.ToString();
                    iPicture.ImageUrl = "https://graph.facebook.com/" + fbr["id"].ToString() + "/picture";
                    iProfile.Text = fbr["name"].ToString();// fbr["name"].value.ToString();
                    lblStatus.Text = "Connected";
                    btnConnect1.Text = "Disconnect";
                    btnConnect1.CssClass = "btnDisconnect";
                    fbr = fb._graph("/me/albums");
                    fbURL.NavigateUrl = fb.getLogoutUrl();
                    if (fbr.ContainsKey("data"))
                    {
                        for (var i = 0; i < fbr["data"].Count; i++)
                            AlbumList.Items.Add(new ListItem(fbr["data"][i.ToString()]["name"].ToString(), fbr["data"][i.ToString()]["id"].ToString()));
                        //or different way:
                        /*foreach (KeyValuePair<String, FacebookResponse> entry in fbr["data"])
                        {

                            AlbumList.Items.Add(new ListItem(entry.Value["name"].ToString()));
                        }*/
                    }
                    while (fbr.ContainsKey("paging") && fbr["paging"].ContainsKey("next"))
                    {
                        fbr = fb._graph(fbr["paging"]["next"].ToString());
                        if (fbr.ContainsKey("data"))
                            for (var i = 0; i < fbr["data"].Count; i++)
                                AlbumList.Items.Add(new ListItem(fbr["data"][i.ToString()]["name"].ToString(), fbr["data"][i.ToString()]["id"].ToString()));
                    }
                    Session.Remove("fb_connect_attempt") ;

                }
                catch (FacebookApiException ex)
                {
                    //todo: log error
                    //probably session expired: we still have the persistent data: user_id and access_token, but the token is no longer valid
                    //best is just to clear the session/cookie OR auto reconnect

                    txtAccessToken.Text = "";
                    iPanel.Visible = false;
                    user = null;
                    if (Session["fb_connect_attempt"] == null)
                    {
                        Session["fb_connect_attempt"] = 1;
                        connectToFB();
                    }
                }
            }
            else
            {
                iPanel.Visible = false;
                lblStatus.Text = "Not Connected";
                fbURL.NavigateUrl = fb.getLoginUrl();
                ViewState["user"] = null;
            }
        }
    }