简体   繁体   English

在Perl脚本中实现自定义Curl身份验证

[英]Implementing custom Curl authentication in Perl script

I am building bot application trying to use perl script to implement the curl request, the issue I have is with Authorization. 我正在构建试图使用Perl脚本实现curl请求的bot应用程序,我遇到的问题是授权问题。

The simple curl command is something like. 简单的curl命令类似。

curl \
  -H 'Authorization: Bearer VM2CKBMXI3AVX2GMYPLBMYFRW3RCHYXS' \
  'https://api.wit.ai/message?v=20160722&q='

I preferred not to use system() calls from the Perl script as there will be plenty of back and forth between the user and the bot. 我宁愿不要使用Perl脚本中的system()调用,因为在用户和机器人之间会有很多来回的回响。

I found this library http://search.cpan.org/~szbalint/WWW-Curl-4.17/lib/WWW/Curl.pm 我发现了这个库http://search.cpan.org/~szbalint/WWW-Curl-4.17/lib/WWW/Curl.pm

I was searching for setopt function in order to find out which params does it accept as my issue is where to put the Authorization param inside the command. 我正在寻找setopt函数,以找出它接受哪些参数,因为我的问题是将Authorization参数放置在命令中的位置。 I found this link http://web.mit.edu/darwin/src/modules/curl/curl/perl/Curl_easy/easy.pm 我发现此链接http://web.mit.edu/darwin/src/modules/curl/curl/perl/Curl_easy/easy.pm

My Script code for now it is like the following: 现在,我的脚本代码如下所示:

use strict;
use warnings;
use WWW::Curl::Easy;

my $curl = WWW::Curl::Easy->new;
my $Authorization="Authorization: Bearer VM2CKBMXI3AVX2GMYPLBMYFRW3RCHYXS";

$curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_URL, 'https://api.wit.ai/message?v=20160721&q=hello');

# A filehandle, reference to a scalar or reference to a typeglob can be used here.
my $response_body;
$curl->setopt(CURLOPT_WRITEDATA,\$response_body);

# Starts the actual request
my $retcode = $curl->perform;

# Looking at the results...
if ($retcode == 0) {
     print("Transfer went ok\n");
     my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
     # judge result and next action based on $response_code
     print("Received response: $response_body\n");
} else {
     # Error code, type of error, error message
     print("An error happened: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n");
}

I just need to know which CURLOPT should I need to use in order to implement authorization. 我只需要知道我应该使用哪个CURLOPT来实现授权。 If you have any idea that will be great. 如果您有什么好主意。

Thanks Eran Gross 谢谢伊兰·格罗斯

If you just want to set the header "Authorization" the option is CURLOPT_HTTPHEADER : 如果只想设置标题“ Authorization”,则选项为CURLOPT_HTTPHEADER

#!/usr/bin/perl
use strict;
use warnings;
use WWW::Curl::Easy;
my $curl = WWW::Curl::Easy->new;
$curl->setopt(CURLOPT_URL, 'http://127.0.0.1/dumprequest.php');
my @headers  = ("Authorization: keygoeshere", "X-Foo: Bah");
$curl->setopt(CURLOPT_HTTPHEADER, \@headers);
$curl->setopt(CURLOPT_HEADER, 1);
my $retcode = $curl->perform;

Gives: 得到:

GET dumprequest.php HTTP/1.1
Host: 127.0.0.1
Accept: */*
Authorization: keygoeshere
X-Foo: Bah

But if you actually want to do HTTP authentication then you would use CURLOPT_USERPWD and CURLOPT_HTTPAUTH . 但是,如果您实际上想执行HTTP身份验证,则可以使用CURLOPT_USERPWDCURLOPT_HTTPAUTH See https://curl.haxx.se/libcurl/c/curl_easy_setopt.html for more info on options. 有关选项的更多信息,请参见https://curl.haxx.se/libcurl/c/curl_easy_setopt.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM