我正在使用STTwitter在 iOS 7 上制作 Twitter 应用程序。
我想在我的应用程序中添加最喜欢的功能。
为了获得用户流,我使用了 STTwitter,然后我想收藏推文。
但是 STTwitter 没有这个功能,所以我决定使用ACAccount
and SLRequest
。
我编码如下所示,在此处引用(在幻灯片 88 和 89 上),但返回了状态代码 400。
我该如何解决?任何帮助都会有很大帮助。
- (IBAction)favButtonTouchDown:(id)sender {
NSLog(@"Now trying to favorite tweet id: %@", _tweetID);
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
_account = [accountStore accountWithIdentifier: ACAccountTypeIdentifierTwitter];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType: accountType
options: nil
completion:^(BOOL granted, NSError *error) {
if (granted)
{
NSArray *accounts = [accountStore accountsWithAccountType: accountType];
if ([accounts count] > 0)
{
_account = accounts[0];
_accountID = _account.identifier;
NSLog(@"Getting account is OK. Account ID is: %@", _accountID);
}
} else {
NSLog(@"Error while getting account.");
}
}];
NSString *urlString =
[NSString stringWithFormat:@"https://api.twitter.com/1.1/favorites/create.json?id=%@", _tweetID];
NSLog(@"URL will be: %@", urlString);
NSURL *url = [NSURL URLWithString: urlString];
// NSDictionary *params = @{@"trim_user" : @"1"};
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:url
parameters:nil];
[request setAccount: _account];
UIApplication *application = [UIApplication sharedApplication];
application.networkActivityIndicatorVisible = YES;
[request performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse,
NSError *error) {
if (responseData) {
NSString *httpErrorMessage = nil;
if (urlResponse.statusCode >= 200 && urlResponse.statusCode < 300) {
NSDictionary *postResponseData =
[NSJSONSerialization JSONObjectWithData: responseData
options: NSJSONReadingMutableContainers
error: NULL];
NSLog(@"SUCCESS! Added Favorite Tweet with ID: %@", postResponseData[@"id_str"]);
} else {
httpErrorMessage =
[NSString stringWithFormat:@"The response status code is %ld",
(long)urlResponse.statusCode];
NSLog(@"HTTP Error: %@", httpErrorMessage);
}
} else {
NSLog(@"ERROR: An error occured while posting %@", [error localizedDescription]);
}
dispatch_async(dispatch_get_main_queue(), ^{
UIApplication *application = [UIApplication sharedApplication];
application.networkActivityIndicatorVisible = NO;
});
}];
}
谢谢。