使用Graph API从Facebook获取用户信息

6
我想从Facebook获取基本用户信息,但以下代码存在问题。
-(void)checkForAccessToken:(NSString *)urlString {
    NSError *error;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"access_token=(.*)&" options:0 error:&error];
    if (regex != nil) {
        **NSTextCheckingResult *firstMatch = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, [urlString length])];
        if (firstMatch) {
            NSRange accessTokenRange = [firstMatch rangeAtIndex:1];
            NSString *accessToken = [urlString substringWithRange:accessTokenRange];
            accessToken = [accessToken stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [_delegate accessTokenFound:accessToken];               
        }**
    }
}

在firstMatch中,我得到了NULL,这就是为什么[_delegate accessTokenFound:accessToken]方法没有被调用的原因。

请问有人能帮我解决这个问题,以便我能够使用Facebook上的用户信息吗?

提前致谢。


你正在使用 Facebook SDK 吗?为什么要那样获取访问令牌,而不是使用官方的 Graph API 呢? - vinzenzweber
通常情况下,您只需要按照Facebook开发手册中的建议进行操作:https://developers.facebook.com/docs/mobile/ios/build/#graph - vinzenzweber
1个回答

3

在使用应用程序ID和凭据进行初始化后,调用fbdid登录方法。

(void)fbDidLogin {

NSLog(@"fbDidLogin");
isFacebookLoaded=YES;


NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
NSLog(@"i am in and my acees token is %@ %@ and call me.CALLshowAloadingView",[facebook accessToken],[facebook expirationDate]) ;
[self.facebook requestWithGraphPath:@"me" andDelegate:self];
if ( [delegate respondsToSelector:@selector(showAloadingView)] ) 
{
    NSLog(@" CALLshowAloadingView");
    // calling delegate method. For this method to function, the delegate should be implemented in the calling class.
    [delegate showAloadingView];                
}


// Inform the delegate that Login is successful
if ( [delegate respondsToSelector:@selector(loginStatus:)] ) {
    // calling delegate method. For this method to function, the delegate should be implemented in the calling class.
    [delegate loginStatus:YES];                 
    return;
}   

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接