gpt4 book ai didi

objective-c - Cocoa 中的 Facebook 身份验证方法可以改进吗?

转载 作者:行者123 更新时间:2023-12-03 17:22:58 24 4
gpt4 key购买 nike

这就是我目前从 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 委托(delegate):

-(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

关于objective-c - Cocoa 中的 Facebook 身份验证方法可以改进吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16309117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com