Desktop Infinite Session Sample

Sep 4, 2009 at 5:12 PM

Hi,

   The desktop sample provided requires the user to log in each time.  This is not good for a screen saver.

   The ConnectToFacebook() Method always pops up a login screen.  Instead, you should be able to pass it the parameters that it is looking for in order to communicate with the facebook API service.  

   This way, as a developer, I know which parameters it is looking for and I will save them for next time.  If the parameters are not provided, then have it pop up the login screen.

 

   Once I get this working I will post the code that is necessary to achieve this.  But meanwhile, does anyone have any information regarding an Infinite Session for a desktop application?

 

   Which parameters do I need to have in order to automatically log in, considering a user approved offline access to the application?

 

As an example, here is the code I am currently using for logging in and saving some data that is visible to me:

 

                .ConnectToFacebook()
                '.ConnectToFacebook()
                My.Settings.SessionKey = FacebookService1.SessionKey
                My.Settings.UserID = FacebookService1.uid
                My.Settings.AuthToken = FacebookService1.API.AuthToken
                My.Settings.Save()

                .ConnectToFacebook()

 

                My.Settings.SessionKey = FacebookService1.SessionKey

                My.Settings.UserID = FacebookService1.uid

                My.Settings.AuthToken = FacebookService1.API.AuthToken

                My.Settings.Save()

 

 

I tried using this data to access the API thereafter without invoking the ConnectToFacebook method with no success.

 

Thank You,

Leeor

Sep 4, 2009 at 6:08 PM

Ok, so now I've learned that the "Secret" code changes.  By the looks of it Facebook wanted me to provide an API AuthToken and a Secret code that IT provided.  Ofcourse, the API AuthToken stays the same, but now I see that the secret code changes.  So storing that info worked for me.  I will put together a better sample after I finish debugging it all.

 

 

            If My.Settings.SessionKey <> String.Empty Then
                .SessionKey = My.Settings.SessionKey
                haveInfo += 1
            End If
            If My.Settings.UserID <> String.Empty Then
                .uid = My.Settings.UserID
                haveInfo += 1
            End If
            If My.Settings.AuthToken <> String.Empty Then
                .API.AuthToken = My.Settings.AuthToken
                haveInfo += 1
            End If
            If My.Settings.Secret <> String.Empty Then
                .Secret = My.Settings.Secret
                haveInfo += 1
            End If

                My.Settings.SessionKey = FacebookService1.SessionKey

                My.Settings.UserID = FacebookService1.uid

                My.Settings.AuthToken = FacebookService1.API.AuthToken

                My.Settings.Secret = FacebookService1.Secret

 

Coordinator
Sep 15, 2009 at 4:31 PM

In the source for version 3.0.  (currently in the DEV branch) I updated the DesktopSession object to work correctly.  It now has built in support to prompt for permissions on login.  Also, if you want to avoid login because you know the sessionkey and sessionsecret of a particular user.  You can just create the DesktopSession object directly and set those.  you can then create an instance of the Api class and pass in the session.

You can then make any calls you need using that instance of the api class.

Sep 30, 2009 at 8:02 PM

Thanks

All is working fine here.

I had a 5 minute limit with jingproject, but I just quickly went over the code I used.  I'll post the code soon also.  The screen saver has been working fantastically for about 1 month now on XP on my netbook  and  on my desktop on  Windows 7.

http://www.screencast.com/users/Leeor/folders/Jing/media/d54e54c3-12bf-4b66-ba22-d99e9db8b8e1

Jan 25, 2010 at 5:20 PM
Edited Jan 25, 2010 at 5:25 PM
rpowers119 wrote:

In the source for version 3.0.  (currently in the DEV branch) I updated the DesktopSession object to work correctly.  It now has built in support to prompt for permissions on login.  Also, if you want to avoid login because you know the sessionkey and sessionsecret of a particular user.  You can just create the DesktopSession object directly and set those.  you can then create an instance of the Api class and pass in the session.

You can then make any calls you need using that instance of the api class.

Hi Ryan,

I have tried to use the desktop as you stated and cannot get it to work.

Can you please supply an example of how to accomplish logging in and being able to use the api to do actions?

Here's what I have tried:

List <Enums.ExtendedPermissions> extPerm = new List<Enums.ExtendedPermissions>();

extPerm.Add(Enums.ExtendedPermissions.offline_access);

extPerm.Add(Enums.ExtendedPermissions.publish_stream);

extPerm.Add(Enums.ExtendedPermissions.status_update);

FacebookSession session = new Facebook.Session.DesktopSession(apiKey, sessionSecret, sessionKey, false, extPerm);

Api api = new Api(session);
var user = api.Users.GetLoggedInUser();

The GetLoggedInUser does return my user id.  My user has granted all of the above ExtendedPermissions to my application.

However, if I try to do any action on the api such as:
api.Stream.Publish("msg", null, null, String.Empty, api.Users.GetLoggedInUser());
string token = api.Auth.CreateToken();

This returns an error:
"A session key must be specified when request is signed with a session secret "

Jan 25, 2010 at 10:24 PM

 

FacebookSession session = new Facebook.Session.DesktopSession(apiKey, sessionSecret, sessionKey, false);
Api api = new Api(session);
string sesKey = api.Session.SessionKey;
                var facebookUserId = api.Users.GetLoggedInUser();
try
{
if(sesKey.Split('-')[1] != facebookUserId.ToString())
throw new ChannelException(EngineAction.Fail, ExceptionCode.NotAuthorized, "User: ");
}
catch (Exception)
{
                    throw new ChannelException(EngineAction.Fail, ExceptionCode.NotAuthorized, "User: ");
}
api.Session.UserId = facebookUserId;
//Display user data captured from the Facebook API.
user facebookUser = api.Users.GetInfo();

I figured out what I was doing wrong, there were a bunch of things incorrect:

  • You only have to pass the extended permissions if you wish to ask the user for extra permissions that they need to have granted.  In my case, I don't need this because that information has already been set up and I don't have a UI
  • By passing the sessionSecret and the sessionKey, you are logging that user in
  • I had also tried calling session.Login() but that is also not needed as the user is already logged in
  • And since the user is already logged in, you do not need to create a token
  • In the api.Stream.Publish, the fifth parameter is supposed to be left as 0, otherwise it will not use your "SessionSecret" or "SessionKey" which is why it returned the error "A session key must be specified when request is signed with a session secret "

Here is a code snippet:

FacebookSession session = new Facebook.Session.DesktopSession(apiKey, sessionSecret, sessionKey, false);

Api api = new Api(session);

string sesKey = api.Session.SessionKey;

        var facebookUserId = api.Users.GetLoggedInUser();

api.Session.UserId = facebookUserId;

//Display user data captured from the Facebook API.

user facebookUser = api.Users.GetInfo();

api.Stream.Publish("msg", null, null, api.Users.GetLoggedInUser().ToString(), 0);

 

The biggest problem for me was that I thought my user was not logged in because of the error that was being returned when I tried to call the publish function.

In order for the GetInfo to work, the api.Session.UserId  must be set.  The  DesktopSession is not setting it, I'm not sure if it should.

Note: I found it a lot easier to figure out why it wasn't working by using reflector to see what parameters/values it is looking for.

--

Christine

Nov 28, 2010 at 5:59 PM

Hi,

I am still working on posting to fan page wall. I can not publish stream using winform. Is this a general problem, is it going on? Could you please help me about that?

Exception message: A session key must be specified when request is signed with a session secret. I tried all suggestions on forums. But It doesnt.

Thank you.