1

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

4

1 回答 1

0

也可以使用 Facebook 为 IOS 提供的 FBGraph API,使用起来非常得心应手,你可以获取新的基于 oAuth 2.0 版本的 FBGraph API 的完整教程,获取教程在这里

于 2013-05-01T10:30:48.620 回答