ConnectSession() fails with no exception

Nov 30, 2009 at 4:26 PM

I'm trying to connect from ASP .NET app (running on IIS6) running on localhost (http://localhost/appname/):

ConnectSession session = new ConnectSession(ApplicationKey, SecretKey);

but session object's IsConnected() returns false.

 

I have double checked ApplicationKey and SecretKey, they are correct. User logins to Facebook successfully, but ConnectSession objects IsConnected() returns false. Strangely enough, <fb:login-button..> control correctly displays "logout" icon, and works as it should work.

 

Any ideas will be appreciated.

Dec 6, 2009 at 11:29 PM

I'm in the same boat... coudn't figure out why IsConnected is returning false after login.

Dec 8, 2009 at 7:08 PM

Problem appeared to be because I was running on localhost. It resolved after I deployed app to a server that uses specified domain.

I also managed to deploy one working app on another PC (localserver), but could not understand why app on one localhost worked, and on another - didn't.

Dec 9, 2009 at 6:22 AM

I am also having this problem.  Is there a workaround to get this running on localhost so i can develop and debug locally before deploying to production?

Dec 15, 2009 at 11:20 AM
Edited Dec 15, 2009 at 11:21 AM

Hi jmoney,

there is  a workaround... ;)

change the FB Application from "http://localhost:port/" to "http://127.0.0.1:port/" and remove the "base domain" property (because 127.0.0.1 will not be accepted as a domain name...)

Then, access your application by entering "http://127.0.0.1:port" in the browser and voila.

 

Happy debugging and a nice day!

Edit:
I just had the same problem and it worked after the mentioned workaround without any problems.

Dec 15, 2009 at 1:31 PM
Edited Dec 15, 2009 at 2:22 PM

Note: I'm using the 3.0 API

MXM_Stops: Could you be more specific? I'm not entirely sure if you're talking about Visual Studio or the Application settings in Facebook Developer.

Edit: In other words I'm not sure where the workaround needs to be applied.

Also, I'm trying to setup a simple connection, so I can see what I can do.
However, the _connectionSession object returns "Cannot fetch the value of field 'LoginCompleted' because information about the containing class is unavailable.".

I'm curious to know what I'm forgetting. I've untill now used the Visual Studio webserver, and have instead setup the IIS (XP Pro) to work with the facebook application. I'm behind a private network, will I need to forward a port to my computer in order to communicate with Facebook?

 

        private const string ApplicationKey = "XXXXXXXXXXX";
        private const string SecretKey = "YYYYYYYYYYYYYY";
        private const long PageId = 12345;
        
        private Api _facebookAPI;

        private ConnectSession _connectSession;

        //private List<EventUser> _eventUsers; 

        protected void Page_Load(object sender, EventArgs e)
        {
            _connectSession = new ConnectSession(ApplicationKey, SecretKey);
            litStatus.Text = "";

            if(!_connectSession.IsConnected())
            {
                litStatus.Text = "Login failed";
            }
            else
            {
                litStatus.Text = "Login succeded";
                _facebookAPI = new Api(_connectSession);
                var fields = _facebookAPI.Pages.GetFields();
                var pageIds = new List<long> {PageId};
                var page = _facebookAPI.Pages.GetInfo(fields, pageIds, null);
               
                if(!Page.IsPostBack)
                {

                }
            }
}

 

 

private const string ApplicationKey = "b6a2c25d3c54a050052a1b42bdaa30ca";
        private const string SecretKey = "f3eb05d52067e73f34c357c7f5db9b5d";
        private const long PageId = 93570655885;
        

        private Api _facebookAPI;

        private ConnectSession _connectSession;

        //private List<EventUser> _eventUsers;

        protected void Page_Load(object sender, EventArgs e)
        {
            _connectSession = new ConnectSession(ApplicationKey, SecretKey);
            litStatus.Text = "";

            if(!_connectSession.IsConnected())
            {
                litStatus.Text = "Innlogging mot applikasjon feilet!";
            }
            else
            {
                litStatus.Text = "Innlogging mot applikasjon lyktes!";
                _facebookAPI = new Api(_connectSession);
                var fields = _facebookAPI.Pages.GetFields();
                var pageIds = new List<long> {PageId};
                var page = _facebookAPI.Pages.GetInfo(fields, pageIds, null);
               
                if(!Page.IsPostBack)
                {

                }
            }
        }
Dec 15, 2009 at 3:04 PM

Sorry for that. I was talking about the Application settings in the facebook.com application (http://www.facebook.com/developers/apps.php).
There you have to change the URL / hostname.

About the error you get:
Do you have all the following:

  • xd_receiver.htm in the root of your web (according to http://wiki.developers.facebook.com/index.php/Cross_Domain_Communication_Channel)
  • at least "facebook.dll" and "facebook.web.dll" in your applications /bin/ directory (or in the reference list of your project)
  • "xmlns:fb="http://www.facebook.com/2008/fbml"" added to the <html> tag of your masterpage (or default.aspx)
  • <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/de_DE" type="text/javascript"></script> at the bottom of your site (before </body>)
  • <script type="text/javascript"> FB.init("YOUR_API_KEY_HERE", "/xd_receiver.htm"); </script> underneath the above script tag

With this, you should be able to add fbml tags (for example the <fb:login-button v="2" autologoutlink="true" onlogin="window.location.reload(true);"></fb:login-button> login button) and the cross communication via JavaScript and the Facebook API should work normally.

Perhaps you try it first without adding the fields stuff (just display if it is connected or not).

 

Hope this helps.

Regards,
Stops

Dec 15, 2009 at 4:04 PM
Edited Dec 15, 2009 at 4:20 PM

Okay, I've checked and fixed the list of things you've mentioned, however, when I try and click on the field which renders an image:

 

<fb:login-button v="2" autologoutlink="true" onlogin="window.location.reload(true);"> 

 

I get the following error:

Invalid argument

The Facebook Connect cross-domain receiver URL (http://localhost:48284/xd_receiver.htm#fname=_opener&%7B%22t%22%3A3%2C%22h%22%3A%22fbCancelLogin%22%2C%22sid%22%3A%220.620%22%7D) must have the application's Connect URL (http://127.0.0.1:48284/) as a prefix. You can configure the Connect URL in the Application Settings Editor.

 

The default.aspx file looks like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="NewsfeedProvider._Default" %>
<%@ Register TagPrefix="uc1" TagName="links" Src="Facebook/Newsfeed/NewsfeedController.ascx" %>  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml" >
<head runat="server">
    <title></title>
    <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
    <script type="text/javascript">FB.init("ik", "/xd_receiver.htm"); </script>
</head>
<body>
<fb:login-button v="2" autologoutlink="true" onlogin="window.location.reload(true);">
    <form id="form1" runat="server">
    <div>
          <uc1:links id="newsfeed" runat="server"/>
    </div>
    </form>
</body>
</html>


Edit: DOH! The error was caused by having http://localhost:48284 instead of http://127.0.0.1:48284

But still I cannot get a connection to facebook.

An interesting error I'm having while trying to connect to Facebook (although I'm recognized as logged in), directly through the script, is that I cannot proceed.

<fb:login-button class=" fb_login_not_authorized FB_login_button FB_ElementReady" onlogin="window.location.reload(true);" autologoutlink="true" v="2">
<a id="RES_ID_fb_login" class="fbconnect_login_button FBConnectButton FBConnectButton_Medium">
<span id="RES_ID_fb_login_text" class="FBConnectButton_Text">
</span>
</a>
</fb:login-button>
This is the HTML I have at startup. login_not_authorized suggests that my user has no rights to login? Which is odd, since my user is tagged as the administrator of the application.

Dec 15, 2009 at 4:13 PM

Can you try to move the two script tags at the bottom of your site (just over the </body></html> tag)?

Would this help perhaps?

Anyway, how do you check if you are "connected" or not? Try it in a second browser (I had the problem that in the FF where I created the Facebook app on facebook.com, I was loggedin and the app was allready accepted. So there was no notification at all that the connect worked oke.)

I hope it is understandable what I'm writing ;)

Dec 15, 2009 at 4:22 PM
Edited Dec 15, 2009 at 4:31 PM

 

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml" >
<head runat="server">
<title></title>

</head>
<body>
<fb:login-button v="2" autologoutlink="true" onlogin="window.location.reload(true);">
<form id="form1" runat="server">
<div>
<uc1:links id="newsfeed" runat="server"/>
</div>
</form>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
<script type="text/javascript"> FB.init("b6a2c25d3c54a050052a1b42bdaa30ca", "/xd_receiver.htm"); </script>
</body>
</html>

Somthing like this?

Edit: I'm not entirely sure how to check wheter I'm connected or not. I tried to connect through a web browser which didn't have an active facebook cookie attached to its session. I ended up typing the username and password in the iframe which popped up, however iframe didn't disapear, and had the 'connect' button available eventhough I was connected to my account.

 

Dec 15, 2009 at 4:27 PM

Jep, right.
Have you tried it in another browser (or logout from Facebook and try again)?

Dec 15, 2009 at 4:31 PM
Edited Dec 16, 2009 at 8:30 AM

WTB googlewave forum for this kind of stuff ;)

To be more specific:

1. With the browser which doesn't have an active facebook cookie in its session, I click the button which the <fb:login-button /> script gets

2. An iframe or a new window pops up

3. I type my name and password

4. I get logged in, the window displays the following: "Connect NewsProvider with Facebook to interact with your friends on this site and to share on Facebook through your Wall and friends' News Feeds."

5. I click connect, nothing happens. I'm not sure if something should happen either.

6. I've tested the sample code 'FBToolkit.Samples.Connect, and I pretty much get the same result. aka the "Cannot fetch the value of field 'LoginCompleted' because information about the containing class is unavailable.".

Dec 15, 2009 at 6:27 PM

In my opinion, after the Step 4 you should be allready connected. If you click a second time the button, nothing should happen.

What happens if you use the above mentioned source:

           _connectSession = new ConnectSession(ApplicationKey, SecretKey);
litStatus.Text = "";

if(!_connectSession.IsConnected())
{
litStatus.Text = "Login failed";
}
else
{
litStatus.Text = "Login succeded";
}

Does the status literal show "login succeded" after you clicked the connect button and supplied the required credentials? If yes, you are connected and you can get the API by "_facebookAPI = new Api(_connectSession);".
After that you can access the API. Here a short example to write out the username:

Facebook.Schema.user loggedinFBUser = _favebookAPI.Users.GetInfo();
litStatus.Text += "; User: " + loggedinFBUser.name;


Why the NewsProvider or this news list is not working, I don't know.. until now, I never worked with this. Sorry.
Dec 15, 2009 at 8:09 PM

The variable _connectSession returns with the error ""Cannot fetch the value of field 'LoginCompleted' because information about the containing class is unavailable."."
And I never get as far as being connected / "login succeded".

May I ask how your network is setup?

Are you behind a private network (using the ip 192.168.1.X or something like that)?

Are you portforwarding anything?

It's not a problem that you're not experienced with the connection part of the toolkit, yes I would have loved to solve the problem right away, but input is often invaluable.

Dec 16, 2009 at 9:47 AM

Good morning,

Ok, I try to post the way I have done the connect authentification. Let's start :)

I made a helper class which handles the Key and Secret loading (since you need a API Key for each hosting you want to run your application, for example you need a Key for each of them: localhost, dev environment, stage environment, live environment). here is the code from my class:

====================================================

public class ConnectAuthentication
{
    protected Facebook.Session.ConnectSession _connectSession;
    protected string _apiKey;
    protected string _apiSecret;
    public string ApiKey
    {
        get
        {
            this.GetApiKeys();
            return this._apiKey;
        }
    }

    public Facebook.Rest.Api Api
    {
        get
        {
            Facebook.Rest.Api _api = null;
            this.LoadConnectSession();

            if (_connectSession.IsConnected())
            {
                _api = new Facebook.Rest.Api(_connectSession);
            }

            return _api;
        }
    }

    public bool IsConnected
    {
        get
        {
            this.LoadConnectSession();

            if (_connectSession != null)
                return _connectSession.IsConnected();
            else
                return false;
        }
    }


    private void LoadConnectSession()
    {
        this.GetApiKeys();
        this._connectSession = new Facebook.Session.ConnectSession(this._apiKey, this._apiSecret);
    }

    private void GetApiKeys()
    {
        // set Facebook API Keys
        // here is some logic where I decide which API Key I take (because for each URL you have to generate a own API Key, for example for localhost, dev env., stage env. and live env.)
        
        this._apiKey = "xxxxxxxxxxxxx";
        this._apiSecret = "yyyyyyyyyyyy";
            

        if (String.IsNullOrEmpty(_apiKey) || String.IsNullOrEmpty(_apiSecret))
            throw new ConfigurationErrorsException("Please add at least one FacebookAPI entry in the web.config!");
    }
}

====================================================

Then, I'm using the following code to establish a API connection in my Page baseclass:


    private ConnectAuthentication _facebookAuth;
    private bool _facebookIsConnected;
    private Facebook.Rest.Api _facebookApi;

    public Facebook.Rest.Api FacebookApi { get { return _facebookApi; } set { _facebookApi = value; } }
    public bool FacebookIsConnected { get { return _facebookIsConnected; } set { _facebookIsConnected = value; } }
    public ConnectAuthentication FacebookAuth { get { return _facebookAuth; } set { _facebookAuth = value; } }


    private void HandleFacebookConnect()
    {
        this._facebookAuth = new ConnectAuthentication();

        if (this._facebookAuth.IsConnected)
        {
            this._facebookIsConnected = true;
            this._facebookApi = this._facebookAuth.Api;
        }
    }

This is the .NET part of connecting the API to a currently open session. The rest is as I described:

  • xd_receiver.htm in the root of your web (according to http://wiki.developers.facebook.com/index.php/Cross_Domain_Communication_Channel)
  • at least "facebook.dll" and "facebook.web.dll" in your applications /bin/ directory (or in the reference list of your project)
  • "xmlns:fb="http://www.facebook.com/2008/fbml"" added to the <html> tag of your masterpage (or default.aspx)
  • <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/de_DE" type="text/javascript"></script> at the bottom of your site (before </body>)
  • <script type="text/javascript"> FB.init("YOUR_API_KEY_HERE", "/xd_receiver.htm"); </script> underneath the above script tag

Now in the Facebook.com application settings (http://www.facebook.com/developers/apps.php) I used the following settings:

  • at the tab "Connect" I only filled out the field "Connect URL" with the value: http://127.0.0.1:5015/

everything else is empty.

And now to my network situation: I'm behind a firewall with a NAT (network address translation) and I'm in a IP range as you mentioned "192.168.1.x". There are no special network needings, nor any Firewall configurations needed. If you get to your web using the selected port, then everything should be fine.

I'm using the Personal Webserver from Visual Studio with the following params:

Command: C:\Programme\Gemeinsame Dateien\Microsoft Shared\DevServer\9.0\WebDev.WebServer.EXE (sorry, I'm using a German Windows XP ;))
Arguments: /port:5015 /path:$(projectDir)\

 

So, I hope with this information, you get to a successful connection. And I hope everything is understandable what I wrote ;)

 

Greetings and have a nice Day,
Stops

Dec 16, 2009 at 3:46 PM

Being a native norwegian with some basic understand in German, I'd say the language barrier is the least of my concerns ;)

I'll get right on testing your suggestions as soon as possible, though I'll probably have to wait till the weekend, I have some overtime at work to get through first .. urgh!

Jan 23, 2010 at 4:13 AM

MXM_Stops' initial suggestion resolved it for me when I had the same issue. Thanks.