0

oAuth 1.0 認証を必要とするアプリケーションを作成しています。クライアントから提供されたコンシューマ キーとコンシューマ シークレットにアクセスできます。私は AFNetworking でそれをやろうとしましたが、うまくいきませんでした。誰かが私に同じことについての良いチュートリアルを提案してくれませんか? 実際、認証されていないユーザーのエラーが発生しています。

4

1 に答える 1

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"];

次に、このクライアントのリクエストを作成し、リクエストの Body を設定します

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];
于 2013-07-07T08:39:23.300 に答える