I have the following code to obtain the twitter Id and name from the Twitter API:
{
NSString *StringUrl = [NSString stringWithFormat:@"https://api.twitter.com/1/users/show.json?screen_name=%@",account.username];
@try {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"GET"];
[request setTimeoutInterval: 15];
[request setURL:[NSURL URLWithString:StringUrl]];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *responseCode = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];
if([responseCode statusCode] != 200){
NSLog(@"Error getting %@, HTTP status code %i", StringUrl, [responseCode statusCode]);
}else{
NSLog(@"Good");
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSString *twitterId = [json objectForKey:@"id_str"];
NSString *twitterName = [json objectForKey:@"name"];
}
}
@catch (NSException *exception) {
NSLog(@"No conection: %@",exception.name);
}
}
It works fine when testing over wifi network; however over cellular network it responds with:
Error getting https://api.twitter.com/1/users/show.json?screen_name=enf_4eva, HTTP status code 400
Thanks in advance.
It seems this was not the best way to do this kind of requests because the request must be authenticated over 3G. With the new SLRequest I could set the account that is doing the request so it works beautifully.