prabirshrestha / FacebookSharp

Facebook Graph API for .Net
Other
30 stars 15 forks source link

AccessToken not valid (Iframe app) #3

Closed jacobh0 closed 14 years ago

jacobh0 commented 14 years ago

I run FacebookAuthenticationResult on the current URL (which does have a signed_request attached to it), I receive an access token, a new Facebook is constructed using that token, and I am calling .Get("/me").Name on that object, and receiving this error:

QueryParseException: An active access token must be used to query information about the current user.] FacebookSharp.Facebook.Get(String graphPath, IDictionary2 parameters, Boolean addAccessToken) +316 FacebookSharp.Facebook.Get(String graphPath, IDictionary2 parameters) +53 FacebookSharp.Facebook.Get(String graphPath) +49 Main.Page_Load(Object sender, EventArgs e) +176 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

I am using a normal web application for my iframe app, not MVC.

jacobh0 commented 14 years ago

I debugged what Facebook is sending me back after ValidateSignedRequest:

{"algorithm":"HMAC-SHA256"}

I have checked your function and it seems to works properly (though I would suggest using a string split on the . instead of substring, it's easier), shouldn't there be more returned from this besides algorithm?

prabirshrestha commented 14 years ago

are u sure u enabled Canvas Session Parameter and OAuth 2.0 for Canvas (beta) in Migration Tab in you application settings to make it work.

in order to get the access token u need to enable Canvas Session Parameter otherwise facebook doesn't send the access token.

prabirshrestha commented 14 years ago

http://yfrog.com/j372988670p here's the picture of my migration settings. i ran FacebookSharp.Samples.CanvasIFrameApplication sample and it works fine.

jacobh0 commented 14 years ago

Yes, both options are turned on.

prabirshrestha commented 14 years ago

according to http://developers.facebook.com/docs/authentication/canvas it must be working, can u try disabling it and cancel the authorzization of the user to your app. and again reopen the application.

jacobh0 commented 14 years ago

It appears my application is not even authorized, does that functionality exist in FacebookSharp, such that I can authorize with extendedpermissions as well?

prabirshrestha commented 14 years ago

you need to authorize with extended permissions. it seems. the reason it was working fine in my sample was coz it had already been authorized by the other mvc sample.

heres the example on how to do authorization. http://wiki.developers.facebook.com/index.php/Authorization_and_Authentication_for_Canvas_Page_Applications_on_Facebook

the direct url is http://www.facebook.com/login.php?v=1.0&api_key=[your_API_key]&next=[your_canvas_page_URL]&canvas=

i will try modifiy the sample application to work with this.

jacobh0 commented 14 years ago

Yea a way to redirect the user if they aren't logged in (or aren't authorized to the app), and to request extended permissions during the authorize would be nice :)

jacobh0 commented 14 years ago

It seems like (atleast in the PHP examples) that they check for the existence of a session (which is the other migration we have enabled), perhaps we could utilize that variable (or lack thereof) to redirect to the login URL? PHP example is below:

http://www.imakeinternet.com/opengraphapi/graph_api_sample.zip

Looking through the facebook class they check the session via the session variable in the query string (if it even exists), and then check it for validity. Perhaps by adding to Facebook.IsSessionValid() and grabbing the session request parameter if present. Also adding a GetLoginUrl function (as seen in the example above) to accompany the GetAuthorizeUrl functions. What do you think?

Also, FacebookAuthenticationResult.IsSuccess should not return true for a blank access token.

I think I might fork and contribute...

prabirshrestha commented 14 years ago

now its working.

you can check the code at. (its in dev branch at the moment) http://github.com/prabirshrestha/FacebookSharp/blob/dev/src/Samples/FacebookSharp.Samples.CanvasIFrameApplication/Controllers/HomeController.cs

incase the user is not logged in u can ask for them to login. but seems like the login is not informative, mite be redirecting to a page and using javascrip sdk to loggin in canvas is a better alternative.

thanks for the IsSuccess and login url suggesstions. and i will be more than happy to see contributions. thanks.

let me know if u find a better solution.

jacobh0 commented 14 years ago

I might try and add to IsSessionValid and copy the functionality in the PHP library, I will experiment with that once I get authorization working in my app and can actually see a session variable.

prabirshrestha commented 14 years ago

note: IsSessionValid() is not meant to check the session from the website like cookies and stuffs like that, its just meant to check whether the access_token and expires_in is valid. rite now i check only for access_token, as i need to convert the unix time expires_in to date and time and check if the access_token has expired.

public bool IsSessionValid() { // todo: not complete yet. return !string.IsNullOrEmpty(AccessToken); }

the main reason is that i want the FacebookSharp.Core.dll to work under client profile. coz of that i have divided the libaries into lot of differnt ones like FacebookSharp.Web.Mvc, FacebookSharp.Winforms

jacobh0 commented 14 years ago

Ok, noted!

prabirshrestha commented 14 years ago

closed