Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Eminences

Microsoft Bing has a brand new Search API. This will force me to modify some of my scripts. Unfortunately enough, Microsoft is not providing any example for Perl (but many in other languages)... guess what: I am having problems in making my script working. The API requiers to pass a key to the query. And this is giving me trubles. This is what I have so far adapting my old script:

my $BingKey="xxxxxxxxxxx";#This is my personal Microsoft API key my $MyBingJsonQuery=" My two Key words"; my $href= 'https://api.cognitive.microsoft.com/bing/v5.0/search[?'. "$ +MyBingJsonQuery". ']'; my $ua= LWP::UserAgent->new(keep_alive); $ua->credentials("api.cognitive.microsoft.com".':443', '', '', $BingKe +y); my $req = HTTP::Request->new(GET => $href); my $res = $ua->request($req); # Check the outcome of the response if ($res->is_success) { #DO SOMETHING }

My solution doesn't work. I guess the problem is in the $ua->credentials statement

Replies are listed 'Best First'.
Re: New Bing Search Api (5)
by Corion (Patriarch) on Nov 21, 2016 at 17:37 UTC

    What makes you guess that?

    What parts of the documentation make you think that?

    Also note that your URL as constructed is not what the documentation suggests to me. The square brackets usually indicate "optional" parts to me and your URL would include them verbatim.

    Likely, your URLs should look like:

    my $href= 'https://api.cognitive.microsoft.com/bing/v5.0/search?'. $My +BingJsonQuery;

    Looking at the examples for the other languages easily confirms that.

    From my reading of the documentation, you need to include a custom header Ocp-Apim-Subscription-Key with your key and you're all set.

    Maybe now is the time to provide a complete example together with the error response you get from Microsoft instead of turning this into a guessing game.

      Thank you for your answer. Here you can find a complete (and slightly modified) script for what I am trying to do. Api Key is not provided. If the script gets a response, it should enter the intended subrutine and I should see my nice message "I could connect to the API" and smile. But I can't get any response, so I get "Impossible retrieving info from Web".

      #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use JSON; my $BingKey="xxx"; my $MyBingJsonQuery = "Hello"; my $href= 'https://api.cognitive.microsoft.com/bing/v5.0/search?' +. $MyBingJsonQuery; print "Connecting to Bing for: $href\n"; my $ua= LWP::UserAgent->new(); my $req = HTTP::Request->new(GET => $href); $req->header($BingKey); my $res = $ua->request($req); # Check the outcome of the response if ($res->is_success) { print "I could connect to the API\n"; my $resp= $ua->get($href); my $data = decode_json($resp->content); my @urls = map { $_->{'Url'} } @{ $data->{d}->{results} }; } else { print "Impossible retrieving info from Web\n"; }

        Have you read the documentation you linked to?

        It also shows you what kinds of errors the API could return.

        Maybe you want to show us what exact kind of error you get back?

        Printing out the full response might show you something that corresponds with the error types of the API:

        print $res->as_string;

        Maybe now is a good time to learn about HTTP and status codes and what the stuff in HTTP::Message and HTTP::Response is all about.

Re: New Bing Search Api (5)
by Anonymous Monk on Nov 21, 2016 at 18:49 UTC

    Just for the sake of reference. This is a working script I could manage to come up with (key must be changed)

    #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use JSON; my $BingKey="your key"; my $MyBingJsonQuery = "Hello"; my $href= 'https://api.cognitive.microsoft.com/bing/v5.0/search?q= +bill gates&count=20&mkt=en-us&safesearch=Moderate'; print "Connecting to Bing for: $href\n"; my $ua= LWP::UserAgent->new(); my $req = HTTP::Request->new(GET => $href); $req->header('content-type' => 'application/json'); $req->header('Ocp-Apim-Subscription-Key'=>'d1490494840c4a1dbdd499a +eee7ded9e'); $req->header('host'=>'api.cognitive.microsoft.com'); my $res = $ua->request($req); print $res->as_string; # Check the outcome of the response if ($res->is_success) { print "I could connect to the API\n"; my $resp= $ua->get($href); my $data = decode_json($resp->content); my @urls = map { $_->{'Url'} } @{ $data->{d}->{results} }; } else { print "Impossible retrieving info from Web\n"; }
      I tried the above and it did not seem to work. But I modified it slightly according to code I had written in Java, and it works. It uses v7 of the Bing API. Here is my slightly modified code below.
      #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use JSON; my $bing_key="PUT_YOUR_KEY_HERE"; my $query = "Hello"; { my $href = "https://api.cognitive.microsoft.com/bing/v7.0/search?q +=$query&Accept=application%2Fjson&offset=0&count=10&subscription-key= +$bing_key"; print "Connecting to Bing for: $href\n"; my $ua= LWP::UserAgent->new(); my $req = HTTP::Request->new(GET => $href); print "-- \$href=$href\n"; $req->header('content-type' => 'application/json'); my $res = $ua->request($req); print $res->as_string; # Check the outcome of the response if ($res->is_success) { print "I could connect to the API\n"; my $resp= $ua->get($href); my $data = decode_json($resp->content); my @urls = map { $_->{'Url'} } @{ $data->{d}->{results} }; } else { print "Impossible retrieving info from Web\n"; } } 1;