我終於找到了問題。
在社交網絡中存在一個錯誤,它認爲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類中構建的異步方法。
希望這會幫助別人!
我只是想精確地說,當我在圖形API瀏覽器中運行該URL時,一切正常。此外憑據是最新的,因爲我可以運行沒有極限參數的查詢... – foOg