iCo*_*777 1 iphone facebook-graph-api ios6
我正在开发基于与服务器通信的iPhone应用程序,我想使用Facebook身份验证机制.
基本上,我认为它应该像这样工作:
我的问题是:如果给定的访问令牌有效,服务器应该如何询问Facebook?我想我应该以某种方式检查令牌是否对我的Facebook应用程序有效.
我已经尝试了很多Facebook查询到图形API,我发现,但没有任何工作像我预期的那样.你能举个例子吗?
我通过这个获得accessToken
NSString * accessToken = [[FBSession activeSession] accessToken];
Run Code Online (Sandbox Code Playgroud)
然后用户可以通过实现此方法获取用户的Facebook数据:
- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error
{
switch (state)
{ /// **************** IT GIVES THIS ACCESS TOKEN *****************/////////////////////////
case FBSessionStateOpen:
{
// https://graph.facebook.com/me?access_token=%@
accessToken = [[FBSession activeSession] accessToken];
NSLog(@"accessToken: %@",accessToken);
NSString *urlList = [NSString stringWithFormat:@"https://graph.facebook.com/me?access_token=%@",accessToken];
NSURL *url1 = [NSURL URLWithString:urlList];
NSURLRequest *urlReqst = [[NSURLRequest alloc] initWithURL:url1];
NSData *dataConnection = [NSURLConnection sendSynchronousRequest:urlReqst
returningResponse:nil
error:nil];
NSString *jsonData = [[NSString alloc] initWithData:dataConnection
encoding:NSUTF8StringEncoding];
NSDictionary *jsonDic = [jsonData JSONValue];
self.appid = [jsonDic valueForKey:@"id"];
self.appusername = [jsonDic valueForKey:@"username"];
self.appfirst_name = [jsonDic valueForKey:@"first_name"];
self.applast_name = [jsonDic valueForKey:@"last_name"];
self.appname = [jsonDic valueForKey:@"name"];
self.appemailId = [jsonDic valueForKey:@"email"];
[self LoginAPI];
if ([self.navigation.presentedViewController isKindOfClass:[LoginPage class]]) {
[self.navigation.presentedViewController dismissViewControllerAnimated:YES completion:nil];
}
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[self.navigation popToRootViewControllerAnimated:NO];
[self showLoginView];
[self fbDidLogout];
break;
default:
break;
}
if (error)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
Run Code Online (Sandbox Code Playgroud)
要通过Facebook登录进入应用程序,您可以浏览此示例
希望对你有帮助...
归档时间: |
|
查看次数: |
799 次 |
最近记录: |