[英]Login and list Facebook friends using Social Framework
有人可以給我一些關於如何整合這一點的指示嗎? 我的目標是獲取安裝我的應用程序(fb app)的朋友列表。 最初,我需要先將用戶登錄到我的應用程序,然后列出已經/尚未安裝該應用程序的朋友。
PS:我不想使用Facebook SDK。 我過去做過噩夢,因為Facebook花費了無數次更改sdk的時間。
===========更新
我已經成功登錄並列出了我的Facebook朋友。 但是現在要列出擁有該應用程序的朋友並列出圖片也很困難。 我嘗試了這個:
網址: https : //graph.facebook.com/me/friends?friends?fields=id,名稱,已安裝,圖片
給我OAuthException : An active access token must be used to query information about the current user.
問題。
我也在API Graph中嘗試過,它工作時沒有提到錯誤。
如果我只嘗試me/friends
完美工作,它將列出我所有的朋友。
首先在您的項目中導入Social,Account,SystemConfiguration框架。 然后在您的.m文件中使用此代碼
-(void)facebook
{
self.accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSString *key = @"XXXXXXXXXXXXX";//get your key form creating new app in facebook app section
NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,@[@"email"],ACFacebookPermissionsKey, nil];
[self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
^(BOOL granted, NSError *e) {
if (granted)
{
NSArray *accounts = [self.accountStore accountsWithAccountType:FBaccountType];
//it will always be the last object with single sign on
self.facebookAccount = [accounts lastObject];
ACAccountCredential *facebookCredential = [self.facebookAccount credential];
NSString *accessToken = [facebookCredential oauthToken];
NSLog(@"Facebook Access Token: %@", accessToken);
NSLog(@"facebook account =%@",self.facebookAccount);
[self get];
[self getFBFriends];
isFacebookAvailable = 1;
} else
{
//Fail gracefully...
NSLog(@"error getting permission yupeeeeeee %@",e);
sleep(10);
NSLog(@"awake from sleep");
isFacebookAvailable = 0;
}
}];
}
-(void)get
{
NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:requestURL parameters:nil];
request.account = self.facebookAccount;
[request performRequestWithHandler:^(NSData *data, NSHTTPURLResponse *response, NSError *error) {
if(!error)
{
NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"Dictionary contains: %@", list );
fbID = [NSString stringWithFormat:@"%@", [list objectForKey:@"id"]];
globalFBID = fbID;
gender = [NSString stringWithFormat:@"%@", [list objectForKey:@"gender"]];
playerGender = [NSString stringWithFormat:@"%@", gender];
NSLog(@"Gender : %@", playerGender);
self.globalmailID = [NSString stringWithFormat:@"%@",[list objectForKey:@"email"]];
NSLog(@"global mail ID : %@",globalmailID);
fbname = [NSString stringWithFormat:@"%@",[list objectForKey:@"name"]];
NSLog(@"faceboooookkkk name %@",fbname);
if([list objectForKey:@"error"]!=nil)
{
[self attemptRenewCredentials];
}
dispatch_async(dispatch_get_main_queue(),^{
});
}
else
{
//handle error gracefully
NSLog(@"error from get%@",error);
//attempt to revalidate credentials
}
}];
self.accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSString *key = @"451805654875339";
NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,@[@"friends_videos"],ACFacebookPermissionsKey, nil];
[self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
^(BOOL granted, NSError *e) {}];
}
-(void)getFBFriends
{
NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me/friends"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:requestURL parameters:nil];
request.account = self.facebookAccount;
[request performRequestWithHandler:^(NSData *data, NSHTTPURLResponse *response, NSError *error) {
if(!error)
{
NSDictionary *friendslist =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
for (id facebookFriendList in [friendslist objectForKey:@"data"])
{
NSDictionary *friendList = (NSDictionary *)facebookFriendList;
[facebookFriendIDArray addObject:[friendList objectForKey:@"id"]];
}
if([friendslist objectForKey:@"error"]!=nil)
{
[self attemptRenewCredentials];
}
dispatch_async(dispatch_get_main_queue(),^{
});
}
else
{
//handle error gracefully
NSLog(@"error from get%@",error);
//attempt to revalidate credentials
}
}];
self.accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSString *key = @"451805654875339";
NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,@[@"friends_videos"],ACFacebookPermissionsKey, nil];
[self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
^(BOOL granted, NSError *e) {}];
}
-(void)accountChanged:(NSNotification *)notification
{
[self attemptRenewCredentials];
}
-(void)attemptRenewCredentials
{
[self.accountStore renewCredentialsForAccount:(ACAccount *)self.facebookAccount completion:^(ACAccountCredentialRenewResult renewResult, NSError *error){
if(!error)
{
switch (renewResult) {
case ACAccountCredentialRenewResultRenewed:
NSLog(@"Good to go");
[self get];
break;
case ACAccountCredentialRenewResultRejected:
NSLog(@"User declined permission");
break;
case ACAccountCredentialRenewResultFailed:
NSLog(@"non-user-initiated cancel, you may attempt to retry");
break;
default:
break;
}
}
else{
//handle error gracefully
NSLog(@"error from renew credentials%@",error);
}
}];
}
我終於明白了,顯然您無法在URL內追加。 您需要在SLRequest
內部的參數中傳遞字段
NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me/friends"];
NSDictionary *param=[NSDictionary dictionaryWithObjectsAndKeys:@"picture,id,name,installed",@"fields", nil];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:requestURL
parameters:param];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.