我已经实现了 facebook SDK,借助它能够使用以下代码获取 facebook 身份验证以及用户信息。
if(!self.accountStore)
self.accountStore = [[ACAccountStore alloc] init];
if(NSClassFromString(@"SLComposeViewController") != nil)
{
ACAccountType *facebookTypeAccount = [self.accountStore
accountTypeWithAccountTypeIdentifier:@"com.apple.facebook"];
NSLog(@"facebookTypeAccount..:%@",facebookTypeAccount);
NSArray *accounts = [self.accountStore accountsWithAccountType:facebookTypeAccount];
if (facebookTypeAccount) {
if ([accounts respondsToSelector:@selector(count)]) {
[self.accountStore requestAccessToAccountsWithType:facebookTypeAccount
options:@{ACFacebookAppIdKey:
@"key", ACFacebookPermissionsKey: @[@"email"]}
completion:^(BOOL granted, NSError
*error) {
if(granted){
NSArray *accounts = [self.accountStore
accountsWithAccountType:facebookTypeAccount];
[self fetchuserinfo];
}
}];
}
-(void)fetchuserinfo
{
NSString *accessToken = [FBSession activeSession].accessToken;
NSString *url=[NSString stringWithFormat:@"https://graph.facebook.com/me"];
NSURL *meurl = [NSURL URLWithString:url];
SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:meurl
parameters:nil];
NSLog(@" setting account %@",_facebookAccount);
merequest.account = _facebookAccount;
[merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
returnedData = responseData;
NSLog(@" meRequest Respnse %@", meDataString);
if (returnedData!=nil) {
if(NSClassFromString(@"NSJSONSerialization"))
{
NSError *error = nil;
id object = [NSJSONSerialization
JSONObjectWithData:returnedData
options:0
error:&error];
if(error) { }
if([object isKindOfClass:[NSDictionary class]])
{
NSDictionary *results = object;
NSString *categoryString=[[results valueForKeyPath:@"picture.data"] objectForKey:@"url"];
NSString *userid=[results objectForKey:@"id"];
if (userid != nil)
{
self.gotUserDetails = YES;
}
NSString *username=[results objectForKey:@"name"];
NSString *email=[results objectForKey:@"email"];
NSUserDefaults *userId=[NSUserDefaults standardUserDefaults];
[userId setObject:userid forKey:@"userid"];
}
}
}
}
但是要从这个登录用户发布提要(https://graph.facebook.com/me/feed/access_token=),我需要访问令牌。如果有人知道如何在 iOS 6.0 中获取访问令牌,请帮助我。
谢谢大家。