我通過Accounts Framework集成了Facebook,我搜索並獲得了一些方法來執行此操作。這是第一次工作,但後來顯示在日誌下面,沒有提供任何信息。Facebook在ios6中獲取用戶信息:必須使用活動訪問令牌來查詢
登錄:
Dictionary contains: {
error = {
code = 2500;
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
};
}
代碼中,我使用
ACAccountStore *_accountStore=[[ACAccountStore alloc] init];;
ACAccountType *facebookAccountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
// We will pass this dictionary in the next method. It should contain your Facebook App ID key,
// permissions and (optionally) the ACFacebookAudienceKey
NSArray * permissions = @[@"email"];
NSDictionary *options = @{ACFacebookAppIdKey :@"my app id",
ACFacebookPermissionsKey :permissions,
ACFacebookAudienceKey:ACFacebookAudienceFriends};
// Request access to the Facebook account.
// The user will see an alert view when you perform this method.
[_accountStore requestAccessToAccountsWithType:facebookAccountType
options:options
completion:^(BOOL granted, NSError *error) {
if (granted)
{
// At this point we can assume that we have access to the Facebook account
NSArray *accounts = [_accountStore accountsWithAccountType:facebookAccountType];
// Optionally save the account
[_accountStore saveAccount:[accounts lastObject] withCompletionHandler:nil];
//NSString *uid = [NSString stringWithFormat:@"%@", [[_accountStore valueForKey:@"properties"] valueForKey:@"uid"]] ;
NSURL *requestURL = [NSURL URLWithString:[@"https://graph.facebook.com" stringByAppendingPathComponent:@"me"]];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:requestURL
parameters:nil];
request.account = [accounts lastObject];
[request performRequestWithHandler:^(NSData *data,
NSHTTPURLResponse *response,
NSError *error) {
if(!error){
NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data
options:kNilOptions error:&error];
NSLog(@"Dictionary contains: %@", list);
userName=[list objectForKey:@"name"];
NSLog(@"username %@",userName);
userEmailID=[list objectForKey:@"email"];
NSLog(@"userEmailID %@",userEmailID);
userBirthday=[list objectForKey:@"birthday"];
NSLog(@"userBirthday %@",userBirthday);
userLocation=[[list objectForKey:@"location"] objectForKey:@"name"];
NSLog(@"userLocation %@",userLocation);
}
else{
//handle error gracefully
}
}];
}
else
{
NSLog(@"Failed to grant access\n%@", error);
}
}];
任何線索朋友什麼錯誤...謝謝。
「self.facebookAccount」是指什麼? – 2013-06-20 10:49:02
ACAccount * facebookAccount; – BhushanVU 2013-06-20 14:37:33
@bhuXan,很好的答案!但爲什麼我無法獲取用戶的照片? – user1673099 2013-12-05 11:35:58