2016-10-31 9 views
1

HotelsPro 웹 서비스에 편안한 요청을 보내는데 문제가 있습니다.인증 자격 증명을 제공하지 않았습니다.

스피 기본 자격 증명이 링크 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=/으로 리디렉션됩니다.이 페이지는 폼 기반 인증 페이지를 찾고 있습니다. 하지만 펄 스크립트에서는 기본 인증을 시도하고 있습니다. Basic Auth vs Form Based Auth의 차이점을 이해하려면 google을 확인하십시오.

이제 Form Based Auth를 수행하려면 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(); 
} 

희망이 있습니다.