【问题标题】:GTM OAuth iOS5 retaining access tokenGTM OAuth iOS5 保留访问令牌
【发布时间】:2012-07-25 22:58:15
【问题描述】:

我不希望用户每次启动应用时都经过授权步骤。相反,我希望应用程序保存来自 3 条腿 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];

【问题讨论】:

    标签: iphone objective-c ios ios5 oauth


    【解决方案1】:

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

    【讨论】:

    • 谢谢!没看wiki。通常我擅长搜索文档...
    猜你喜欢
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 2012-07-20
    相关资源
    最近更新 更多