OAuth 1.0在iOS中使用消费者密钥和消费者密钥

时间:2013-07-06 03:21:10

标签: iphone ios oauth

我正在制作一个需要oAuth 1.0身份验证的应用程序。我可以访问客户端提供的消费者密钥和消费者密钥。我曾试图用AFNetworking做到这一点,但它还不顺利。 有人可以建议我一个很好的教程。实际上我收到了未经身份验证的用户的错误。

1 个答案:

答案 0 :(得分:3)

我找到了这个问题的答案。首先,我使用了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];