无法验证OAuth签名和令牌twitter集成ios GTM OAuth



我正在使用以下代码处理应用程序中的twitter集成。

- (IBAction)signInWithTwitter:(id)sender {    
NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/request_token"];
NSURL *accessURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"];
NSURL *authorizeURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/authorize"];
NSString *scope = @"http://api.twitter.com/";
GTMOAuthAuthentication *auth = [self authForTwitter];
[auth setCallback:@"http://www.noop.com/OAuthCallback"];
GTMOAuthViewControllerTouch *viewController;
viewController = [[GTMOAuthViewControllerTouch alloc] initWithScope:scope
                                                             language:nil
                                                      requestTokenURL:requestURL
                                                    authorizeTokenURL:authorizeURL
                                                       accessTokenURL:accessURL
                                                       authentication:auth
                                                       appServiceName:@"CK12: Twitter"
                                                             delegate:self
                                                     finishedSelector:@selector(viewController:finishedWithAuth:error:)];
}
- (GTMOAuthAuthentication *)authForTwitter {
GTMOAuthAuthentication *auth = [[GTMOAuthAuthentication alloc] initWithSignatureMethod:kGTMOAuthSignatureMethodHMAC_SHA1
                                                     consumerKey:TWITTER_CONSUMER_KEY
                                                      privateKey:TWITTER_CONSUMER_SECRET];
[auth setServiceProvider:@"Twitter"];
return auth;
}

我的问题是,如果我更改设备时间,即延迟1小时,那么我会收到以下错误:

Error Domain=com.google.HTTPStatus Code=401 and error message is : failed to validate oauth signature and token .

所以有人能建议如何解决这个问题吗。如果系统时间是错误的,那么我也想让它工作。

我终于找到了解决方案。

我们收到此错误的原因之一是"无法验证OAuth签名和令牌"当系统时间错误时。因为OAuth请求带有系统时间戳参数,所以当设备时间不在twitter服务器时间的5分钟内时,我们会得到"验证OAuth签名和令牌失败"。

如果设备时间不对,有两种方法可以使其工作。

1.向api.twitter.com上的一个端点发出HTTP HEAD请求——您将在响应中获得一个Date HTTP标头,指示twitter所理解的当前时间。然后将其转换为epoch时间,并通过确定的偏移量调整oauth_timestamp值。

2.有一个名为iOS-ntp链接的小型iOS库:http://code.google.com/p/ios-ntp。使用此项可以获得当前的精确时间。

之后,我只在下面的方法中设置OAuth对象的时间戳

- (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;
}

就是这样。快乐编码:)

最新更新