I'm making a Twitter application on iOS 7 using STTwitter .
I want to add function of favorite in my application.
To getting user stream I used STTwitter, then I want to favorite tweet.
But STTwitter doesn't have such function, so I decided to use ACAccount
and SLRequest
.
I coded as shown below, referencing here (on slide 88 and 89), but status code 400 was returned.
How can I fix it? Any helps would greatly help.
- (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;
});
}];
}
Thanks.