2012-10-10 28 views
0

我在iOS上遇到了圖形API的一些問題。 我正嘗試從用戶獲取所有FB相冊。我注意到,默認情況下,圖形API回答25個第一個元素,然後他們提供下一個和/或先前的url來查詢其餘的。Facebook中的限制偏移量iOS圖形API

我的問題是,我需要在一次鏡頭中查詢每個元素(不僅是前25個)。

我嘗試使用Facebook文檔中解釋的極限參數,但我得到一個空數據數組作爲響應。當我刪除限制參數時,我可以抓住25個第一個元素。 當我嘗試使用時,Facebook API的行爲方式類似,直到今天或直到昨天。

這裏是我使用的網址: https://graph.facebook.com/me/albums?limit=0 0應該是沒有限制,我試過99999的結果。

我想知道如果有人已經從圖譜API有這種奇怪的行爲?

謝謝你的幫助!

+0

我只是想精確地說,當我在圖形API瀏覽器中運行該URL時,一切正常。此外憑據是最新的,因爲我可以運行沒有極限參數的查詢... – foOg

回答

0

我終於找到了問題。

在社交網絡中存在一個錯誤,它認爲Apple總是在url字符串的末尾附加字符串"?access_token=[ACCESS_TOKEN]"

據此,如果您之前在URL字符串中添加了參數,則URL不是有效的,因爲您將有兩個「?」在字符串中。

爲了避免這種情況,我使用NSURLConnection的類來管理這樣的連接:

NSString *appendChar = [[url absoluteString] rangeOfString:@"?"].location == NSNotFound ? @"?" : @"&"; 
NSString *finalURL = [[url absoluteString] stringByAppendingFormat:@"%@access_token=%@", appendChar, self.facebookAccount.credential.oauthToken]; 
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:finalURL]]; 
NSURLResponse *response; 
NSError *error; 
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

if (error) 
    [self.delegate facebookConnection:self didFailWithError:error]; 
else 
{ 
    NSError *jsonError; 
    NSDictionary *resultDictionnary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError]; 

    if (jsonError) 
     [self.delegate facebookConnection:self didFailWithError:jsonError]; 
    else if ([resultDictionnary valueForKey:@"error"]) 
    { 
     NSDictionary *errorDictionary = [resultDictionnary valueForKey:@"error"]; 
     NSError *facebookError = [NSError errorWithDomain:[errorDictionary valueForKey:@"message"] code:[[errorDictionary valueForKey:@"code"] integerValue] userInfo:nil]; 
     [self.delegate facebookConnection:self didFailWithError:facebookError]; 
    } 
    else 
     [self.delegate facebookConnection:self didFinishWithDictionary:resultDictionnary httpUrlResponse:response]; 
} 

首先,我測試的字符串在參數字符的存在和我追加正確的。 我給你作爲獎勵處理錯誤的方式。

我還是用社會框架來獲取證書和連接的用戶:

NSDictionary *accessParams = @{ACFacebookAppIdKey:kFacebookAppID, ACFacebookPermissionsKey:@[@"email", @"user_photos", @"user_activities", @"friends_photos"]}; 
ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

[accountStore requestAccessToAccountsWithType:accountType options:accessParams completion:^(BOOL granted, NSError *error) 
{ 
    if (granted) 
    { 
     NSArray *facebookAccounts = [accountStore accountsWithAccountType:accountType]; 

     if ([facebookAccounts count] > 0) 
     { 
      self.facebookAccount = [facebookAccounts objectAtIndex:0]; 
      self.accessToken = self.facebookAccount.credential.oauthToken; 
      [self.delegate facebookConnectionAccountHasBeenSettedUp:self]; 
     } 
    } 
    else 
     [self.delegate facebookConnection:self didFailWithError:error]; 
}]; 

在這段代碼中,我不處理多個Facebook賬號,但你可以很容易地變換段來處理它在自己的辦法。此外,連接是同步發送的,因爲我使用GCD來避免阻塞我的接口,但是您可以實現NSURLConnection類中構建的異步方法。

希望這會幫助別人!