使用LWP :: UserAgent进行身份验证的Perl中的POST API [英] POST API in Perl using LWP::UserAgent with authentication
问题描述
我正在尝试在perl中使用POST方法向API发送信息。
I am trying to use POST method in perl to send information to an API.
我想调用下面的api,它需要以下输入:
URI: https://www.cryptopia.co.nz/api/SubmitTrade
I would like to call the below api which requires following inputs: URI: https://www.cryptopia.co.nz/api/SubmitTrade
输入参数为: -
市场:交易的市场符号,例如'DOT / BTC'(如果提供'TradePairId'则不需要)
Market: The market symbol of the trade e.g. 'DOT/BTC' (not required if 'TradePairId' supplied)
TradePairId:交易的Cryptopia交易对象标识符,例如'100'(如果提供'市场'则不需要)
TradePairId: The Cryptopia tradepair identifier of trade e.g. '100' (not required if 'Market' supplied)
类型:交易类型,例如'买入'或'卖出'
Type: the type of trade e.g. 'Buy' or 'Sell'
价格:为硬币支付的价格或价格,例如0.00000034
Rate: the rate or price to pay for the coins e.g. 0.00000034
金额:要购买的硬币数量,例如: 123.00000000
Amount: the amount of coins to buy e.g. 123.00000000
请告诉我如何从perl调用这个api?
请求结构:
Please can you tell me how I can call this api from perl ? Request Structure:
REQUEST_SIGNATURE:API_KEY +POST+ URI + NONCE + HASHED_POST_PARAMS
REQUEST_SIGNATURE: API_KEY + "POST" + URI + NONCE + HASHED_POST_PARAMS
API_KEY:您的Cryptopia api密钥
API_KEY: Your Cryptopia api key
URI:请求uri。例如 https://www.cryptopia.co.nz/Api/SubmitTrade
URI: the request uri. e.g. https://www.cryptopia.co.nz/Api/SubmitTrade
HASHED_POST_PARAMS:发布参数的Base64编码MD5哈希
HASHED_POST_PARAMS: Base64 encoded MD5 hash of the post parameters
NONCE:每个请求的唯一指标。
NONCE: unique indicator for each request.
所以问题是如何加入这个api https:/ /www.cryptopia.co.nz/api/SubmitTrade 并通过身份验证传递参数并检查返回结果是否成功?
So question is how do I join this api https://www.cryptopia.co.nz/api/SubmitTrade and pass the arguments to it with authentication and check if returned result is success?
结果示例:
{
"Success":true,
"Error":null,
"Data":
{
"OrderId": 23467,
"FilledOrders": [44310,44311]
}
}
推荐答案
我没有Cryptopia帐户来测试这个,但这至少可以让你更接近到一个工作原型。
I don't have a Cryptopia account to test this, but this might at least get you closer to a working prototype.
加载所需模块并创建 LWP :: UserAgent
object。
Load the required modules and create a LWP::UserAgent
object.
use strict;
use warnings;
use LWP::UserAgent;
use JSON;
use Digest::MD5 qw(md5);
use Digest::SHA qw(hmac_sha256);
use MIME::Base64;
use URI::Encode qw(uri_encode);
my $api_key = 'PUBLIC KEY';
my $api_private_key = 'PRIVATE KEY';
my $ua = LWP::UserAgent->new;
生成请求
棘手的部分正在生成授权标头。您的帐户的API密钥将进行base64编码 - 因此在使用它来签署请求时会调用 decode_base64()
。
my $url = "https://www.cryptopia.co.nz/api/GetTradeHistory";
my %req = (
Market => 'DOT/BTC',
);
my $nonce = int(rand(1000000));
my $post_data = encode_json(\%req);
my $post_data_enc = encode_base64(md5($post_data), "");
my $encoded_url = lc(uri_encode($url, encode_reserved => 1));
my $req_signature = sprintf("%sPOST%s%s%s", $api_key, $encoded_url, $nonce, $post_data_enc);
# Sign request signature with private key.
my $req_signature_hmac = encode_base64(hmac_sha256($req_signature, decode_base64($api_private_key)));
# Generate value for 'Authorization' header field.
my $auth_header_value = sprintf("amx %s:%s:%s", $api_key, $req_signature_hmac, $nonce);
备注......
- 空字符串作为第二个参数传递给
encode_base64()
- 以防止它在输出中附加换行符。 -
uri_encode()
来自URI :: Encode
模块用于编码URL。 / li>
- 你可能想为nonce使用替代源或随机生成器。
- An empty string is passed as the second argument to
encode_base64()
- to prevent it from appending a new-line to the output. uri_encode()
from theURI::Encode
module is used the encode the URL.- You might want to use an alternative source or random generator for the nonce.
您可以显式创建 HTTP :: Request
对象并将其传递给 LWP :: UserAgent
的 request()
方法,但有一个 post()
方便的方法,为你做这一切。
You could create a HTTP::Request
object explicitly and pass that to LWP::UserAgent
's request()
method, but there is a post()
convenience method which does all this for you.
这里的例子使用内容
参数来设置内容邮寄正文,并同时设置授权
和 Content-Type
标题。
The example here uses the Content
parameter to set the content of the post body, and sets the Authorization
and Content-Type
headers at the same time.
my $response = $ua->post($url,
Content => $post_data,
'Content-Type' => 'application/json',
Authorization => $auth_header_value
);
die "Request failed: ", $response->content unless $response->is_success();
print $response->content, $/;
检查回复
可能是足以通过调用 $ response-> is_success()
方法检查上面的LWP Response对象。尽管如此,如果您想明确检查是否成功,只需解码JSON响应 - 使用 $ resp = decode_json($ response-> decoding_content)
。然后检查生成的哈希中的相关键。
Checking the response
It may be enough to check the LWP Response object as above - by invoking the $response->is_success()
method. Nonetheless, if you want to explicitly check for success, just decode the JSON response - using $resp = decode_json($response->decoded_content)
. Then examine the relevant keys in the resulting hash.
这篇关于使用LWP :: UserAgent进行身份验证的Perl中的POST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!