【问题标题】:OAuth 1.0 using Consumer Key and Consumer Secret in iOS在 iOS 中使用消费者密钥和消费者秘密的 OAuth 1.0
【发布时间】:2013-07-04 04:12:39
【问题描述】:

我正在制作一个需要 oAuth 1.0 身份验证的应用程序。我可以访问客户提供的消费者密钥和消费者秘密。我曾尝试使用 AFNetworking 来实现,但效果不佳。 有人可以建议我一个很好的教程。实际上,未经身份验证的用户出现错误。

【问题讨论】:

    标签: iphone ios oauth


    【解决方案1】:

    我找到了这个问题的答案。首先,我同样使用了 AFNetworking 和 AFOauth1Client。

     AFOAuth1Client * client = [[AFOAuth1Client alloc] initWithBaseURL:[NSURL  URLWithString:<your URL>] key:<Your Consumer Key> secret:<Your Consumer Secret>];
    [client setOauthAccessMethod:@"POST"];
    [client setSignatureMethod:AFHMACSHA1SignatureMethod];
    [client setDefaultHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];
    

    接下来我为这个客户端创建了一个请求并设置了请求的正文

    NSMutableURLRequest * request =[client requestWithMethod:@"POST" path:<URL Path> parameters:nil];
                                            //here path is actually the name of the method    
    
    
    [request setHTTPBody:[<Your String Data> dataUsingEncoding:NSUTF8StringEncoding]];
    

    然后我使用了AFHttpRequestOperation和上一步中的请求

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    
    [client registerHTTPOperationClass:[AFHTTPRequestOperation class]];
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    
            // Success Print the response body in text
            NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
    
            //parsing the xml Response
            NSXMLParser * parser= [[NSXMLParser alloc] initWithData:responseObject];
    
            [parser setDelegate:self];
    
            [parser parse];
    
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            //Something Went wrong..
            NSLog(@"Error: %@", error);
        }];
        [operation start];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 2015-12-05
      • 2016-04-14
      • 2011-09-08
      • 2014-08-23
      • 2012-03-10
      • 1970-01-01
      相关资源
      最近更新 更多