可可的Facebook认证方法可以改进吗?

时间:2013-04-30 21:18:23

标签: objective-c xcode facebook cocoa

这就是我目前从facebook收到我的access_token的方式,目前正在为mac创建一个简单的Facebook客户端。

  NSString *clientId = @"********";
  NSString *scope = @"read_stream";

  NSString *urlString = [NSString stringWithFormat:@"https://www.facebook.com/dialog/oauth?"
                         "client_id=%@"
                         "&redirect_uri=https://www.facebook.com/connect/login_success.html"
                         "&scope=%@"
                         "&response_type=token", clientId, scope];

    NSURLRequest *req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];

    NSURLConnection *conn = [[[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES] autorelease];

    [req release];

然后我拿起didReceiveResponse代表:

-(void)connection:(NSURLConnection *)connection didReceiveResponse: (NSURLResponse *)response
{
    NSString *urlString = [[response URL] absoluteString];

    int accessTokenStartPosition = [urlString rangeOfString:@"access_token="].location + 13;
    int accessTokenEndPosition   = [urlString rangeOfString:@"&"].location;

    NSRange accessTokenRange = NSMakeRange(accessTokenStartPosition, accessTokenEndPosition - accessTokenStartPosition);
    NSString *accessToken = [urlString substringWithRange: accessTokenRange];

    int expiryStartPosition = [urlString rangeOfString:@"expires_in="].location + 11;
    int expiryEndPosition   = urlString.length;

    NSRange expiryRange = NSMakeRange(expiryStartPosition, expiryEndPosition - expiryStartPosition);
    NSString *expiryTime = [urlString substringWithRange: expiryRange];

    NSLog(@"Test: %@", accessToken);
    NSLog(@"Test: %@", expiryTime);
}

不确定这是否是最好的方法,而不愿使用适用于iOS的Facebook SDK,是否有更好的方法或我是否在正确的轨道上?

1 个答案:

答案 0 :(得分:0)

您也可以使用Facebook为IOS提供的FBGraph API,使用非常方便和方便,您可以获得新的基于oAuth 2.0的FBGraph API版本的完整教程,Get the tutorial here