Facebook login through API code (no login window popup)

Mar 9, 2010 at 4:14 PM

I want to develop a server application that uses the APIKey, APISecret, email and password information stored in database, to periodically connect to facebook and retrieve everything that is written in the "wall" (a simple example). This server application can be deployed in several places and connect to different facebook accounts.

Every code examples that I have found, use a authentication browser window and the user enters manually the login + passwd information. In my case, such approach is undesirable because I want the server application to achieve the login session through the "API" code.

Using:
 - Facebook developer toolkit
 - C#

I haven't found any information regarding this issue and I would like to know if it can be done ? Suggestions / workarounds ?


Thank you very much.

 

Mar 9, 2010 at 8:42 PM

Provided you have an infinite session key, which is acquired by asking the user for the offline_access permission, you can use it by setting it to the Facebook API Session object like so:

_facebookAPI.Session.SessionKey = user.FacebookOfflineAccessSessionKey;
_facebookAPI.Session.UserId = (long)user.FacebookId;
_facebookAPI.Session.Login();

 

 

 

Mar 16, 2010 at 1:34 PM
Hi, I am also trying to automatically login without window popup. but everytime it displays the login popup window. if i do not use session.login(), method then it always says invalid session key. I store the session key (session.SessionKey) and session secret (session.SessionSecret) the first time the users login together with uid. will appreciate if you (someone) can let me know what i am doing wrong. regards, rnv
clukic wrote:

Provided you have an infinite session key, which is acquired by asking the user for the offline_access permission, you can use it by setting it to the Facebook API Session object like so:

_facebookAPI.Session.SessionKey = user.FacebookOfflineAccessSessionKey;
_facebookAPI.Session.UserId = (long)user.FacebookId;
_facebookAPI.Session.Login();

 

 

 

Mar 18, 2010 at 7:11 PM
clukic wrote:

Provided you have an infinite session key, which is acquired by asking the user for the offline_access permission, you can use it by setting it to the Facebook API Session object like so:

_facebookAPI.Session.SessionKey = user.FacebookOfflineAccessSessionKey;
_facebookAPI.Session.UserId = (long)user.FacebookId;
_facebookAPI.Session.Login();

 

 

 

 Which version of the toolkit are you using because I don't see FacebookOfflineAccessSessionKey property available for the user object.  I am trying to find a way to capture the permanent session key but I though I had to use the Auth class to get it.

Mar 19, 2010 at 6:09 PM

Sorry for not being more clear. That's your locally stored offline access key. It's the session key you get returned to you after requesting the offline access permission.

  • Request offline access permission
  • Grab the new session key and store it (along with the users user id) in the database
  • Restore the session key and user if to the session object
  • Use the facebook api as if the user was logged in, sometime later when they are not
Mar 19, 2010 at 7:17 PM
clukic wrote:

Sorry for not being more clear. That's your locally stored offline access key. It's the session key you get returned to you after requesting the offline access permission.

  • Request offline access permission
  • Grab the new session key and store it (along with the users user id) in the database
  • Restore the session key and user if to the session object
  • Use the facebook api as if the user was logged in, sometime later when they are not

Thanks for the reply clukic!  When a user first comes to my site they see the facebook connect button and once they have authenticated I display a link for extended permissions but I think that's where my problem is, I'm getting the session information to early and not capturing the session which has the offline_access info.  I am using the fb:login and the promppermission controls but is there a way to tell the fb:login controll to prompt for permissions once they have authenticated so when control is returned to my page I will have the correct session info? 

Mar 19, 2010 at 8:12 PM

I've seen quite a bit of discussion on these forums and the facebook dev forums about that issue. Facebook doesn't seem to set the cookies in a timely manner,

 I haven't encountered the issue  because I'm using the FBJS library and manually redirecting the page only after I have a session and the cookies are set. Sorry I can't be of more help.

Mar 19, 2010 at 9:22 PM
clukic wrote:

I've seen quite a bit of discussion on these forums and the facebook dev forums about that issue. Facebook doesn't seem to set the cookies in a timely manner,

 I haven't encountered the issue  because I'm using the FBJS library and manually redirecting the page only after I have a session and the cookies are set. Sorry I can't be of more help.

 Thanks again, you have been a big help.  I think I will try using the FBJS and see if that will give me what I need. 

Mar 20, 2010 at 4:35 PM

I concur that the whole session-management-in-cookies thing is unreliable.  I have had good results using the new alpha javascript client library for prompting for extended permissions and obtaining the user's session key.  The new alpha javascript client library is cookieless by default.  It also has the (minor) advantage of not requiring the cross domain receiver file setup.

Mar 20, 2010 at 4:51 PM

Where I've run into a dead end is restoring my session on the server as I described, but then trying to pass it back to the client. I haven't played around with the new alpha library yet, is there any facility to do that? I've tried creating my session and calling promotesession on the server, and tried passing my session to the client and calling set_session in FBJS, but no luck...

Apr 6, 2010 at 1:16 PM
clukic wrote:

Sorry for not being more clear. That's your locally stored offline access key. It's the session key you get returned to you after requesting the offline access permission.

  • Request offline access permission
  • Grab the new session key and store it (along with the users user id) in the database
  • Restore the session key and user if to the session object
  • Use the facebook api as if the user was logged in, sometime later when they are not

I am using a desktop application, and try to avoid the login screen during the next login and after the offline access permission was granted.

The offline access is working fine, as long as the user is logged in to Facebook. The problem is that whenever the user is logging out of Facebook (through Internet Explorer), and my desktop application tries to logging-in, the login screen pops up again (because the user was logged-out from Facebook through Internet Explorer).

I tried to set the session key before the login as you suggested and it didn't help.

Any ideas how I should proceed?

 

Apr 20, 2010 at 12:54 PM

Just wanted to share a working piece of code, based on the 3.1 beta, and intended for use in a WPF application.

I have tested logging in/out from Internet Explorer, and it does not have any effect on the below code.

The keys to get this working for me, were:

  1. Include the offline_access flag in permissions
  2. Save SessionKey and SessionSecret after calling Login (manual login), and reuse these next time (automatic login)
  3. And, most importantly, only call Session.Login when user needs to manually login the first time!

Remember to replace [Application Key] with the correct one from your Facebook application.

Hope this helps someone out there!

 

        Dim lst As List(Of Enums.ExtendedPermissions) = New List(Of Enums.ExtendedPermissions)

        lst.Add(Enums.ExtendedPermissions.read_stream)
        lst.Add(Enums.ExtendedPermissions.publish_stream)
        lst.Add(Enums.ExtendedPermissions.offline_access)

        Dim strSessionKey As String = My.Settings.SessionKey

        If String.IsNullOrEmpty(strSessionKey) = True Then
            strSessionKey = Nothing
        End If

        Dim strSessionSecret As String = My.Settings.SessionSecret

        If String.IsNullOrEmpty(strSessionSecret) = True Then
            strSessionSecret = Nothing
        End If

        If strSessionKey Is Nothing OrElse strSessionSecret Is Nothing Then
            Session = New DesktopSession("[Application Key]", True, lst)

            Session.Login()

            My.Settings.SessionKey = Session.SessionKey
            My.Settings.SessionSecret = Session.SessionSecret
            My.Settings.FacebookUserId = Session.UserId

            My.Settings.Save()

        Else
            Session = New DesktopSession("[Application Key]", strSessionSecret, strSessionKey, True, lst)
            Session.UserId = My.Settings.FacebookUserId

        End If

        Api = New Api(Session)

Apr 27, 2010 at 4:57 PM
Edited Apr 27, 2010 at 5:08 PM

I have been trying to get facebook integration to work without luck for a week now. Any help is appreciated. 

To give a background, ours is a web application that integrates with Twitter and FB. Both these social networks are used to allow users to receive notifications from our system. 

Also, my application works slightly differently. There is a account linking page, where the user is asked to link their FB account with our application. Thereafter, the application is making posts to the users stream when certain events occur. These events take place when the user is not logged in and hence the requirement for offline_access. 

I used your code block in the account linking section to allow users to add our application to thier profile. It works great minus the extended permissions. FB never prompts the user to allow extended permissions. What am I missing here?

LarsenLP wrote:

Just wanted to share a working piece of code, based on the 3.1 beta, and intended for use in a WPF application.

I have tested logging in/out from Internet Explorer, and it does not have any effect on the below code.

The keys to get this working for me, were:

  1. Include the offline_access flag in permissions
  2. Save SessionKey and SessionSecret after calling Login (manual login), and reuse these next time (automatic login)
  3. And, most importantly, only call Session.Login when user needs to manually login the first time!

Remember to replace [Application Key] with the correct one from your Facebook application.

Hope this helps someone out there!

 

        Dim lst As List(Of Enums.ExtendedPermissions) = New List(Of Enums.ExtendedPermissions)

        lst.Add(Enums.ExtendedPermissions.read_stream)
        lst.Add(Enums.ExtendedPermissions.publish_stream)
        lst.Add(Enums.ExtendedPermissions.offline_access)

        Dim strSessionKey As String = My.Settings.SessionKey

        If String.IsNullOrEmpty(strSessionKey) = True Then
            strSessionKey = Nothing
        End If

        Dim strSessionSecret As String = My.Settings.SessionSecret

        If String.IsNullOrEmpty(strSessionSecret) = True Then
            strSessionSecret = Nothing
        End If

        If strSessionKey Is Nothing OrElse strSessionSecret Is Nothing Then
            Session = New DesktopSession("[Application Key]", True, lst)

            Session.Login()

            My.Settings.SessionKey = Session.SessionKey
            My.Settings.SessionSecret = Session.SessionSecret
            My.Settings.FacebookUserId = Session.UserId

            My.Settings.Save()

        Else
            Session = New DesktopSession("[Application Key]", strSessionSecret, strSessionKey, True, lst)
            Session.UserId = My.Settings.FacebookUserId

        End If

        Api = New Api(Session)

 

Sep 30, 2010 at 2:50 PM

Hi,

I'm also having a problem in requesting extendPermission.

 I need to request extended permission for email on loading the application.

 I wasted lots of hours finding a solution.

I'm very much Thankful if you can give me the code to prompt for extendPermission.

Thanks,

Madhu