SLRequest on Twitter give me this: HTTP response status: 403 Error 0error (null)

2.6k views Asked by At

i'm using SLRequest with twitter, and until a few days ago everything worked fine with the code below but now the response is empty and the error description is null, this is the code i always use:

ACAccountStore *accountStoreTw = [[ACAccountStore alloc] init];

ACAccountType *accountTypeTw = [accountStoreTw accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStoreTw requestAccessToAccountsWithType:accountTypeTw options:NULL completion:^(BOOL granted, NSError *error) {
    if(granted) {

        FMDatabase *db = [FMDatabase databaseWithPath:[self databasePath]];

        if ([db open]) {
            FMResultSet *tw_name_q = [db executeQuery:@"SELECT tw_username FROM settings WHERE id = 1"];

            NSString *tw_name = @"";

            while ([tw_name_q next]) {
                tw_name = [tw_name_q stringForColumn:@"tw_username"];
            }

            if (![tw_name isEqualToString:@""]) {

                NSArray *accountsArray = [accountStoreTw accountsWithAccountType:accountTypeTw];

                for (ACAccount *tw_account in accountsArray) {

                    if ([[tw_account username] isEqualToString:tw_name]) {

                        SLRequest* twitterRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                                       requestMethod:SLRequestMethodPOST
                                                                                 URL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"]
                                                                          parameters:[NSDictionary dictionaryWithObject:msg_post forKey:@"status"]];

                        [twitterRequest setAccount:tw_account];

                        [twitterRequest performRequestWithHandler:^(NSData* responseData, NSHTTPURLResponse* urlResponse, NSError* error) {
                            NSLog(@"text response: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
                            NSLog(@"error: %@",error.localizedDescription);
                            NSString *output = [NSString stringWithFormat:@"HTTP response status: %i Error %d", [urlResponse statusCode],error.code];
                            NSLog(@"%@ error %@", output,error.description);

                        }];

                        break;
                    }
                }
            }
        }

        [db close];
    }

}];

now this log give me this:

text response: 
error: (null)
403 Error 0 error (null)

what is the problem? any idea to fix it?

2

There are 2 answers

2
Terence Eden On BEST ANSWER

There has been a recent change to the Twitter API. You can now only call it using HTTPS.

You should ensure that the URL you / your library is using starts with

https://api.twitter.com/1.1/

(Notice the extra s after the http.)

0
GeneCode On

There is another reason why you're getting 403 code if you are testing this code repeatedly on the same twitter account:

For each update attempt, the update text is compared with the authenticating user’s recent Tweets. Any attempt that would result in duplication will be blocked, resulting in a 403 error. Therefore, a user cannot submit the same status twice in a row.

When testing, be sure to change the status text to a different ones each time. Or you can write simple code to produce random text for the status (just for debugging time) to prevent the 403 error code.

Example:

NSDictionary *message = @{@"status": [NSString stringWithFormat:@"Testing app %u", arc4random() % 100]};

SLRequest* twitterRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                                       requestMethod:SLRequestMethodPOST
                                                                                 URL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"]
                                                                          parameters: message];