2013-07-06 43 views
0

我正在製作一個需要oAuth 1.0身份驗證的應用程序。我可以訪問客戶給出的消費者密鑰和消費者密鑰。我試圖用AFNetworking來做,但它沒有很好地完成。 有人可以建議我一個很好的教程。其實我得到一個未經身份驗證的用戶錯誤。OAuth 1.0在iOS中使用Consumer Key和Consumer Secret

回答

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]; 
作出請求的請求的主體