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