One of the reason we get this error "Failed to validate OAuth signature and token" is when system time is wrong. Because OAuth request carry system timestamp parameters with it, when the device time is not within the 5 minutes of Twitter's server time, we get "Failed to validate OAuth signature and token".
If this is the reason, and you want it to make work if the system time is wrong also, then there are two ways to make it work:
Make HTTP HEAD request to an endpoint on api.twitter.com -- you'll get a Date HTTP header in the response that indicates the current time understood by Twitter. You would then convert this to epoch time and adjust your oauth_timestamp
values by a determined offset.
There's a small iOS library ios-ntp. Use this to get the accurate current time.
After that set the timestamp of OAuth object in the following method
- (GTMOAuthAuthentication *)authForTwitter {
GTMOAuthAuthentication *auth = [[GTMOAuthAuthentication alloc] initWithSignatureMethod:kGTMOAuthSignatureMethodHMAC_SHA1
consumerKey:TWITTER_CONSUMER_KEY
privateKey:TWITTER_CONSUMER_SECRET];
[auth setServiceProvider:@"Twitter"];
NSDate * currentdate = //GET ACCURATE DATE HERE ;
[auth setTimestamp:[NSString stringWithFormat:@"%f",[currentdate timeIntervalSince1970]]];
return auth;
}
That's it... Happy coding :)