2011-06-28 74 views
1

我正在嘗試將OAuth配置到我的iPhone應用程序中以連接到另一個Web服務,但我遇到了問題。如何在我的iPhone應用程序中使用OAuth?

我已經下載了谷歌gtm-oauth文件並將它們添加到我的項目中。

- (GTMOAuthAuthentication *)myCustomAuth { 
    NSString *myConsumerKey = @"2342343242"; // pre-registered with service 
    NSString *myConsumerSecret = @"324234234242"; // pre-assigned by service 

    GTMOAuthAuthentication *auth; 
    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 = @"RunKeeper"; 

    return auth; 
} 
- (void)signInToCustomService { 

    NSURL *requestURL = [NSURL URLWithString:@"https://runkeeper.com/apps/token"]; 
    NSURL *accessURL = [NSURL URLWithString:@"https://runkeeper.com/apps/token"]; 
    NSURL *authorizeURL = [NSURL URLWithString:@"https://runkeeper.com/apps/authorize"]; 
    NSString *scope = @"http://example.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:@"http://www.example.com/OAuthCallback"]; 

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

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

這是我試圖連接與API:http://developer.runkeeper.com/healthgraph/registration-authorization

回答

2

首先,閱讀了關於OAuth的。它與普通的登錄/ pw類型認證有點不同。

其次,你可以在http://code.google.com/p/gtm-oauth/

+0

好的使用谷歌的OAuth庫,我下載了所有相關文件,在我的項目它們添加到文件夾,但現在我該怎麼辦? – Jon

相關問題