2016-10-31 20 views
1

HotelsProウェブサービスに安らかなリクエストを送信する際に問題があります。認証資格情報が提供されていません

IAMが、基本的な資格情報を使用して、このリンクhttps://api-test.hotelspro.com:443に要求を送信しようとしているが、私は毎回助けてください

sub getJSONdata { 

    my ($SupplierXMLServer, $message, $compressed,$timeOut) =(); 
     ($SupplierXMLServer, $message, $compressed,$timeOut) = @_; 

    $SupplierXMLServer='https://api-test.hotelspro.com/api/v2/search/?destination_code=20b05&checkin=2016-11-09&checkout=2016-11-12&currency=USD&client_nationality=PS&pax=2'; 

    my $username = "Epilgrim"; 
    my $password = "xxxxxxxxxx"; 

    use LWP::UserAgent; 
    my $userAgent = LWP::UserAgent->new(agent =>"1"); 
     $userAgent->credentials('https://api-test.hotelspro.com:443', 'api', $username , $password); 
     $userAgent->timeout($timeOut) if($timeOut); # in seconds 
    use HTTP::Request::Common; 
    my $response = ''; 
    if($compressed){ 
     $response = $userAgent->request(GET $SupplierXMLServer, 
              Content_Type => 'application/json', 
              Accept_Encoding => "gzip,deflate", 
              Content => $message); 
    } 
    else{ 
     $response = $userAgent->request(GET $SupplierXMLServer, 
              Content_Type => 'application/json', 
              Content => $message); 
    } 
    return $response->error_as_HTML unless $response->is_success; 
     #return $response->content; 

    if($compressed){ 
    return $response->decoded_content; 
    } 

    else{ 
     return $response->content; 
     } 
    } 

をたどるように、この資格情報がbrowser.Myコードに取り組んでいる「認証資格情報が提供されていなかった」エラーが出ます私は正しいコードを書いて正しい方法で要求を送信して有効な応答を得る。

+0

'credentials'の呼び出しで、プロトコルを削除してみることができますか?' hostname:port'の形式でなければなりません - つまり ''api-test.hotelspro.com:443'' http: /stackoverflow.com/questions/1799147/why-dont-my-lwpuseragent-credentials-work – ardavey

答えて

1

指定されたリンクはhttps://api-test.hotelspro.com/login/?next=/にリダイレクトされ、フォームベースの認証ページが表示されます。しかし、あなたのPerlスクリプトでは、基本認証を試みています。 Basic Auth vs Form Based Authの違いを理解するにはGoogleをご確認ください。

フォームベースの認証を実行するには、LWPのラッパーであるが、より便利な方法を提供するWWW :: Mechanizeを使用する方がよいでしょう。右WWW::Mechanize's official help page:

#!/usr/bin/perl -w -T 

use strict; 
use WWW::Mechanize; 

my $login = "login_name"; 
my $password = "password"; 
my $folder = "folder"; 

my $url = "http://img78.photobucket.com/albums/v281/$login/$folder/"; 

# login to your photobucket.com account 
my $mech = WWW::Mechanize->new(); 
$mech->get($url); 
$mech->submit_form(
    form_number => 1, 
    fields  => { password => $password }, 
); 
die unless ($mech->success); 

# upload image files specified on command line 
foreach (@ARGV) { 
    print "$_\n"; 
    $mech->form_number(2); 
    $mech->field('the_file[]' => $_); 
    $mech->submit(); 
} 

この情報がお役に立てば幸いですから、フォームベース認証のためのサンプルコードを以下に示します!

関連する問題