Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Greetings great monks,

I wonder if you might be able to help answer a quick question for me with regard to Net::OAuth and POST data. I've been trying to integrate with the TradeKing API, which uses OAuth 1.0a, but I'm running into a problem I can't seem to figure out with regard to POST data. From what I understand, Net::OAuth assumes that all data will be in the form of key/value pairs that are provided to "extra_params", but I can't seem to find a way to provide a single block of POST data so that it can be included in the signing process.

Is there a way using Net::OAuth to include POST data/content so that it will be picked up by the signing process?

More specifically, here are the documentation references I'm using and sample code:

https://developers.tradeking.com/documentation/watchlists-post
Using POST method to deliver key/value pairs, which I've been able to get working.

#!/usr/bin/env perl use Modern::Perl '2015'; use Math::Random::MT 'rand'; use Net::OAuth; use URL::Encode 'url_encode'; use HTTP::Request; use LWP::UserAgent; use JSON::XS; use Data::Dumper 'Dumper'; my %params = ( id => 'watchlist-' . time, symbols => 'AAPL,MSFT', ); my $content = join( '&', map { url_encode($_) . '=' . url_encode( $params{$_} ) } keys %params ); my $oauth = Net::OAuth->request('protected resource')->new( consumer_key => '...', consumer_secret => '...', token => '...', token_secret => '...', protocol_version => Net::OAuth::PROTOCOL_VERSION_1_0A, signature_method => 'HMAC-SHA1', timestamp => time, nonce => int( rand( 2**32 ) ), request_url => 'https://api.tradeking.com/v1/watchlists.json' +, request_method => 'POST', extra_params => \%params, ); $oauth->sign; my $request = HTTP::Request->new( $oauth->request_method, $oauth->request_url, [ 'Authorization' => $oauth->to_authorization_header, 'Content-Type' => 'application/x-www-form-urlencoded', ], $content, ); say $request->as_string; my $response = LWP::UserAgent->new->request($request); if ( not $response->is_success ) { say $response->message; say $response->content; } else { say Dumper( JSON::XS->new->decode( $response->content ) ); }

https://developers.tradeking.com/documentation/accounts-id-orders-post
Using POST data (a single block of content), which I've been unable to get working.

#!/usr/bin/env perl use Modern::Perl '2015'; use Math::Random::MT 'rand'; use Net::OAuth; use URL::Encode 'url_encode'; use HTTP::Request; use LWP::UserAgent; use JSON::XS; use Data::Dumper 'Dumper'; my $content = '<?xml version="1.0" encoding="UTF-8"?> <FIXML xmlns="http://www.fixprotocol.org/FIXML-5-0-SP2"> <Order TmInForce="0" Typ="2" Side="1" Px="13" Acct="38619105"> <Instrmt SecTyp="CS" Sym="AAPL"/> <OrdQty Qty="1"/> </Order> </FIXML>'; my $oauth = Net::OAuth->request('protected resource')->new( consumer_key => '...', consumer_secret => '...', token => '...', token_secret => '...', protocol_version => Net::OAuth::PROTOCOL_VERSION_1_0A, signature_method => 'HMAC-SHA1', timestamp => time, nonce => int( rand( 2**32 ) ), request_url => 'https://api.tradeking.com/v1/accounts/3861910 +5/orders.json', request_method => 'POST', extra_params => { POSTDATA => $content }, ); $oauth->signature_elements( [ @{ $oauth->signature_elements }, $conten +t ] ); my $request = HTTP::Request->new( $oauth->request_method, $oauth->request_url, [ 'Authorization' => $oauth->to_authorization_header, 'Content-Type' => 'application/x-www-form-urlencoded', 'TKI_TRADEPASS' => 'myPass', 'TKI_OVERRIDE' => 'true', ], $content, ); say $request->as_string; my $response = LWP::UserAgent->new->request($request); if ( not $response->is_success ) { say $response->message; say $response->content; } else { say Dumper( JSON::XS->new->decode( $response->content ) ); }

What's going on in the second code sample here is that I'm trying to get Net::OAuth to "see" the POST data and include it in the creation of the signature. So far, that hasn't happened, so the response I get back from the API is an authentication failure.

Any suggestions would be most appreciated. Thanks very much.


In reply to Net::OAuth and POST data by gryphon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (9)
    As of 2020-11-23 17:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?