GTM OAuth iOS5保留访问令牌

时间:2012-07-25 12:44:16

标签: iphone objective-c ios ios5 oauth

我不希望用户每次启动应用程序时都要通过授权步骤。相反,我希望应用程序从三脚架OAuth 1.0a进程中保存访问令牌,并使用它们访问受保护的资源。

这是我到目前为止所做的事情,并且每次都要求我授权(不要登录)。

有什么想法吗?请帮忙。使用GTMOauth库。

    - (void)signIn
{
    activityIndicator.hidden = NO;
    [activityIndicator startAnimating];

    NSURL *requestURL = [NSURL URLWithString:@"http://webservice/oauth/request_token/"];
    NSURL *accessURL = [NSURL URLWithString:@"http://webservice/oauth/access_token/"];
    NSURL *authorizeURL = [NSURL URLWithString:@"http://webservice/oauth/authorize/"];
    NSString *scope = @"http://webservice";

    auth = [self getAuthForWebservice];

    [auth setCallback:@"http://webservice/OAuthCallback"];    

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

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

    [self navigationController].navigationBarHidden = YES;    
}


- (void)viewController:(GTMOAuthViewControllerTouch *)viewController
      finishedWithAuth:(GTMOAuthAuthentication *)auth
                 error:(NSError *)error {
    if (error == nil) {
        [self performSelector:@selector(doAnAuthenticatedAPIFetch)];

    }
}

- (void)doAnAuthenticatedAPIFetch {
    NSString *urlStr = @"http://webservice/resource/";

    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [auth authorizeRequest:request];

    NSError *error = nil;
    NSURLResponse *response = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response
                                                     error:&error];

1 个答案:

答案 0 :(得分:2)

gtm-oauth控制器将令牌保存到钥匙串。当应用程序再次启动时,从钥匙串中读回来,如here所示。