Facebook授权不起作用(iOS Native app)

时间:2012-08-03 11:41:22

标签: ios facebook json facebook-graph-api single-sign-on

我正在尝试在我的应用中实现Facebook登录并且它正在运行(我正在获取访问令牌),但每当我尝试发出图形请求时,我都会收到以下消息:我需要一个有效访问权限令牌,以便向图API发出请求:

Error Domain=facebookErrDomain Code=10000 "The operation couldn’t be completed. (facebookErrDomain error 10000.)" UserInfo=0x6a99fa0 {
    error=<CFBasicHash 0x6a99050 [0x1582b48]>{
        type = mutable dict, count = 3,
            entries =>
                2 : <CFString 0x6a99340 [0x1582b48]>{contents = "type"} = <CFString 0x6a994d0 [0x1582b48]>{contents = "OAuthException"}
                3 : <CFString 0x6a99470 [0x1582b48]>{contents = "message"} = <CFString 0x6a993d0 [0x1582b48]>{contents = "An active access token must be used to query information about the current user."}
                6 : <CFString 0x6a52270 [0x1582b48]>{contents = "code"} = 2500
        }
    }

发出请求的代码看起来像那样(也许我需要在这里添加访问令牌?):

[facebook requestWithGraphPath:@"me" andDelegate:self];

现在我知道访问令牌是正常的,因为在init方法中我调用了一个方法,在必要时更新它:

[facebook extendAccessTokenIfNeeded];

我还在实现extendAccessTokenIfNeeded

的委托方法
-(void)fbDidExtendToken:(NSString *)accessToken expiresAt:(NSDate *)expiresAt {
    NSLog(@"token extended"); //This NSLog hasn't been called, so I know the extending access token method wasn't fired...
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:accessToken forKey:@"FBAccessTokenKey"];
    [defaults setObject:expiresAt forKey:@"FBExpirationDateKey"];
    [defaults synchronize];
} 

现在我在这里失踪了什么?我是否需要将访问令牌添加到图形API请求中?如果是这样,怎么样?

顺便说一句,我也知道,如果我向公共路径发出图形请求,它就能完美地运行。

1 个答案:

答案 0 :(得分:0)

刚遇到同样的问题,我不知道为什么,但原生的facebook SDK似乎没有用他们的令牌代替GET请求。可能是你麻烦了吗?我用了以下解决方法:

 NSMutableDictionary *params = [NSMutableDictionary  dictionaryWithObjectsAndKeys:
                                   _facebook.accessToken, @"access_token",
                                   nil];

    [_facebook requestWithGraphPath:@"me/music"
                          andParams:params
                      andHttpMethod:@"GET"
                        andDelegate:self];

UPD 的答案适用于[_facebook requestWithGraphPath:@"me" andParams:params andHttpMethod:@"GET" andDelegate:self],但不适用于[facebook requestWithGraphPath:@"me" andDelegate:self];