2013-05-08 15 views
1

我想獲取facebook好友個人資料picture.I也給它的access_token。我得到了Facebook中的格式錯誤的訪問令牌在ios 6

我在網上搜索了很多,但沒有得到任何好的解決方案。

我的代碼如下:

- (IBAction)advancedButtonPressed:(id)sender { 

    self.accountStore = [[ACAccountStore alloc]init]; 
    ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

    //NSString *key = @"xxxxxx"; 

    NSString *key = @"xxxxxx"; 
    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]; 
        NSLog(@"facebook account =%@",self.facebookAccount); 

       ACAccountCredential *fbCredential = [self.facebookAccount credential]; 
       accessToken = [fbCredential oauthToken]; 
       NSLog(@"Facebook Access Token: %@", accessToken); 

       [self get]; 
      } else { 
       //Fail gracefully... 
       NSLog(@"error getting permission %@",e); 

      } 
     }]; 

} 


-(void)fr 
{ 
NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me/friends?fields=id,name,picture,first_name,last_name,gender&access_token=BAAFcRQZCZAUeUBAICacM2q1V5UzRHee6xHGnbEdu19v6CZBIEDJmTwHBKtMZBwtVuFiEeuXqt4yKeSkeI17QZBGaZCeszBfOKdbZAfR4E9RN2nFqHalKPXZBga96WQCU8CSNG4ZCJCK1V7JIbqZAPRgNl6mjjO4Ns1CEaLpbXUYtum1CXbNqTtT82YpVVKoZCmMEBpqHfwYMx1OKu9RZBsjPZA6DlhKA1uGyU32EGJ8QOQZASJVQZDZD"]; 

    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) 
     { 
      list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

      NSLog(@"Dictionary contains data: %@", list); 
      if([list objectForKey:@"error"]!=nil) 
      { 
       [self attemptRenewCredentials]; 
      } 
      dispatch_async(dispatch_get_main_queue(),^{ 
       nameLabel.text = [list objectForKey:@"username"]; 
      }); 
     } 
     else{ 
      //handle error gracefully 
      NSLog(@"error from get%@",error); 
      //attempt to revalidate credentials 
     } 

    }]; 
} 

但我總是得到的錯誤是這樣的:

error =  { 
     code = 190; 
     message = "Malformed access token BAAFcRQZCZAUeUBAICacM2q1V5UzRHee6xHGnbEdu19v6CZBIEDJmTwHBKtMZBwtVuFiEeuXqt4yKeSkeI17QZBGaZCeszBfOKdbZAfR4E9RN2nFqHalKPXZBga96WQCU8CSNG4ZCJCK1V7JIbqZAPRgNl6mjjO4Ns1CEaLpbXUYtum1CXbNqTtT82YpVVKoZCmMEBpqHfwYMx1OKu9RZBsjPZA6DlhKA1uGyU32EGJ8QOQZASJVQZDZD?access_token=BAAFcRQZCZAUeUBAHBZBSdTL9wWFDOVthxj78bBJlrNEkPiKHdxZA1HaRmO9dtBTJdsZCtZCu2vaE5ByZAdK7Ox30BpIR0KTg0ZC6pi1IOE0KnZCZBSxqbycG0C6Ws5Ct4ferL3G0VU1wIfDypKekFM3zB4r5gAiUREQ66Yuz2Fu2mp5NGqZCBE1p357H41P2lKOAQN9EZAmu4q3SRtjMuVIw4dtdC00l4RhMuDWTOIUQlO54bgZDZD"; 
     type = OAuthException; 
    }; 
} 

我不能夠解決這個問題。幫我!

回答

2

如果您使用的是SLRequest,它會自動爲您添加訪問令牌,而且您本身也不應該有任何請求參數。相反,你應該這樣做:

NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me/friends"]; 

SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook 
             requestMethod:SLRequestMethodGET 
                URL:requestURL 
              parameters:@{@"fields":@"id,name,picture,first_name,last_name,gender"}]; 
+0

謝謝您的回覆!是的,這是工作!但是,我如何獲取使用該應用的朋友列表? – user2357044 2013-05-09 03:48:03

+0

將「已安裝」添加到您的字段列表中。 – 2013-05-09 18:22:21

0

你也可以用這個代替使用SLRequest.It的也是

   NSURL *meurl = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/me?access_token=%@",fbAccessToken]]; 

      NSURLRequest *req = [[NSURLRequest alloc]initWithURL:meurl]; 

      [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 

        NSString *userDataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

      }];