check if my facebook app is allready authorized

My question is how to check if my FaceBook app is already authorized for posts by the user, can't find any info on that.

I'm using:

 Facebook* facebook = [[Facebook alloc] initWithAppId:@"1234567"];
 [facebook authorize:[NSArray arrayWithObjects:@"read_stream", @"offline_access",nil] delegate:self];

A dialog pops up asking me to authorize the app, when done i'm all fine, can do a:

 [facebook dialog:@"feed" andDelegate:self];

to post notes on that app.

But, if the user blocks or removes the app i want to do the authorize again before showing the dialog for posting, can't find a way of getting that kind of info before calling authorize.

Any help is appreciated.

Thanks.


I had to deal with this issue too.

When calling the dialog method, you send a delegate that should conform to FBDialogDelegate, which has a method that is called when the dialog fails to load due an error. But in the case the app has been unauthorized, the dialog shows a login screen to the user, but after setting the user and password, a second form appears, letting the user know that an error has occurred. The delegate is also called, but the error received just states that he method has failed with no exact reason why, or even an error number. This method should be called with the correct error, before anything, so the application could act accordingly.

So I found a work around, maybe this is not the best way, but it certainly works. Any call that you do to the Facebook graph api via a request, will fail if the app has been unauthorized by the user. So what I did was to check that before calling the feed dialog method.

Add the following line where you need to test if the app is still authorized:

if ([facebook isSessionValid])
    //isSessionValid only checks if the access token is set, and the expiration date is still valid. Lets make a call and see if we really are authorized to post to this user from this app.     
    [facebook requestWithGraphPath:@"me" andDelegate:self];
else
    //authorize Facebook connect

This will just call the method that returns the basic information from the user. If everything is fine, the following method will be called from the delegate:

- (void)request:(FBRequest *)request didLoad:(id)result
{
   //Everything is ok. You can call the dialog method. It should work.
}

If the app has been unauthorized by the user, the following method from the delegate will be called:

- (void)request:(FBRequest *)request didFailWithError:(NSError *)error;
{        
    NSString *type = [[[error userInfo] objectForKey:@"error"] objectForKey:@"type"];

    if (type)
    {
        if ([type isEqualToString:@"OAuthException"]) //aha! 
        {
            //user has unauthorized the app, lets logout from Facebook connect. Also clear the access and expiration date tokens
            [facebook logout:self];


            //Call the authorize method again. Or let the user know they need to authorize the app again.
        }
    }
}

So, as I said before, not the best way, but gets the job done. Hopefully, Facebook will add a method to check for this specific scenario, or add a new method to the delegate that deals with the unauthorized app issue.


I'm not sure how exactly to do it with Facebook's SDK, but you can use FQL to query the permissions. The query URL would look something like

https://api.facebook.com/method/fql.query?query=SELECT+uid,+read_stream,+offline_access+FROM+permissions+WHERE+uid=me()&access_token=...

It looks like requestWithMethodName:andParams:andHttpMethod:andDelegate: passing fql.query as the method is the way to go, as long as you can arrange for isSessionValid to be true (or somehow supply access_token in the params yourself).

链接地址: http://www.djcxy.com/p/6038.html

上一篇: Mathematica笔记本的版本控制

下一篇: 检查我的Facebook应用程序是否已获得授权