2014-01-31 103 views
0

有人能給我一些關於如何整合這一點的指針嗎?我的目標是獲得安裝我的應用程序的朋友列表(fb應用程序)。最初我需要首先將用戶登錄到我的應用程序,並列出有/未安裝該應用程序的朋友。登錄並使用社交框架列出Facebook朋友

PS:我不想使用Facebook SDK。由於facebook做過無數次改變sdk,我曾經做過噩夢。

=========== UPDATE

我已經成功登錄,並列出我的Facebook好友。但現在問題列出我的朋友誰擁有應用程序和列表圖片以及。我嘗試這樣做:

網址:https://graph.facebook.com/me/friends?friends?fields=id,name,installed,picture

這給我OAuthException : An active access token must be used to query information about the current user.問題。

我也試過在API Graph,它的工作原理沒有提到錯誤。

如果我只嘗試me/friends完美的作品,它會列出所有的朋友。

+0

這怎麼可能?我只得到朋友的總數。 – Teddy

回答

3

我終於得到它的代碼,顯然你不能在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]; 
4

首先導入項目中的Social,Account,SystemConfiguration框架。 然後使用上your.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); 
     } 
    }]; 
} 
+0

謝謝。但得到Facebook的朋友正在工作,但我的問題是讓安裝我的Facebook應用程序的朋友。 – HelmiB

+0

https://graph.facebook.com/user_id/friends?fields=id,name,installed 您是否在Url中嘗試過此操作 – morroko

+0

OAuthException:必須使用活動訪問令牌來查詢有關當前用戶的信息。問題。 此錯誤是由於訪問令牌來解決這個問題,你必須得到訪問令牌 – morroko