简体   繁体   English

FaceBook iOS - 检查我的Facebook应用程序是否已经全部授权

[英]FaceBook iOS - 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. 我的问题是如何检查我的FaceBook应用程序是否已被用户授权发布,无法找到任何相关信息。

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. 调用dialog方法时,发送一个符合FBDialogDelegate的委托,该委托有一个方法,当对话由于错误而无法加载时调用该方法。 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. 如果应用程序未被用户授权,您通过请求对Facebook图形API执行的任何调用都将失败。 So what I did was to check that before calling the feed dialog method. 所以我做的是在调用feed对话框方法之前检查它。

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. 希望Facebook将添加一种方法来检查这个特定的场景,或者向代表添加一个新的方法来处理未经授权的应用程序问题。

I'm not sure how exactly to do it with Facebook's SDK, but you can use FQL to query the permissions . 我不确定如何使用Facebook的SDK完成它,但您可以使用FQL查询权限 The query URL would look something like 查询URL看起来像

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). 它看起来像requestWithMethodName:andParams:andHttpMethod:andDelegate:传递fql.query作为方法就行了,只要你可以安排isSessionValid为true(或者以某种方式在params中提供access_token )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM