这是我目前从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,是否有更好的方法,或者我是否走上正轨?

最佳答案

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

10-04 21:09