Tripit + GTMOAuthViewControllerTouch

时间:2012-02-14 13:04:10

标签: iphone cocoa-touch ios4 oauth

我有一个iPhone应用程序。我正在尝试使用GTMOAuthViewControllerTouch添加tripit支持。它使用4 NSURL:

NSString *myConsumerKey = kTripItAPIKey;        // pre-registered with service
NSString *myConsumerSecret = kTripItAPISecret;  // pre-assigned by service

GTMOAuthAuthentication *auth = [[[GTMOAuthAuthentication alloc] initWithSignatureMethod:kGTMOAuthSignatureMethodHMAC_SHA1
                                                                            consumerKey:myConsumerKey
                                                                             privateKey:myConsumerSecret] autorelease];

// setting the service name lets us inspect the auth object later to know
// what service it is for
auth.serviceProvider = @"Tripit";

NSURL *requestURL = [NSURL URLWithString:@"https://api.tripit.com/oauth/request_token"];
NSURL *accessURL = [NSURL URLWithString:@"https://api.tripit.com/oauth/access_token"];
NSURL *authorizeURL = [NSURL URLWithString:@"https://m.tripit.com/oauth/authorize"];
NSString *scope = @"https://api.tripit.com/scope";

GTMOAuthAuthentication *auth = [self myCustomAuth];

// set the callback URL to which the site should redirect, and for which
// the OAuth controller should look to determine when sign-in has
// finished or been canceled
//
// This URL does not need to be for an actual web page
[auth setCallback:@"https://api.tripit.com/OAuthCallback"];

// Display the autentication view
GTMOAuthViewControllerTouch *viewController = [[[GTMOAuthViewControllerTouch alloc] initWithScope:scope
                                                                                         language:nil
                                                                                  requestTokenURL:requestURL
                                                                                authorizeTokenURL:authorizeURL
                                                                                   accessTokenURL:accessURL
                                                                                   authentication:auth
                                                                                   appServiceName:@"AppName"
                                                                                         delegate:self
                                                                                 finishedSelector:@selector(viewController:finishedWithAuth:error:)] autorelease];

[[self navigationController] pushViewController:viewController
                                       animated:YES];

我正在阅读官方文档,我发现了请求,访问和自动调整URL,但它没有谈论任何有关“范围”和OAuthCallback url?的内容。当我尝试访问时,我从Tripit站点收到“访问请求失败”消息。怎么了? :-(

1 个答案:

答案 0 :(得分:0)

如果其他人遇到同样的问题,我会回答这个问题。

您需要稍微修改Google的项目才能将其与TripIt配合使用。 TripIt希望您在请求网址中发送oauth_token以及oauth_callback。记录UIWebview正在加载的请求,以准确查看发送到TripIt服务器的内容。

在您的情况下,它可能看起来像这样:

https://m.tripit.com/oauth/authorize?oauth_token=<THE TOKEN HERE>

什么时候看起来像这样:

https://m.tripit.com/oauth/authorize?oauth_token=<THE TOKEN HERE>&oauth_callback=<YOUR CALLBACK URL>

将回调网址附加到原始请求,您应该看到登录/授权屏幕加载,而不是“访问请求失败”。

此外,这可能有助于任何人对与TripIt和OAuth 1.0集成感到困惑。 http://blog.andydenmark.com/2009/03/how-to-build-oauth-consumer.html