http://www.perlmonks.org?node_id=996948

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

I am trying to use the Bing's Search API to fetch results however authentication is not working properly. Here's the code I have written:

#!/usr/bin/perl use warnings; use WWW::Mechanize; use Crypt::SSLeay; $mech = WWW::Mechanize->new(); $url = "https://api.datamarket.azure.com/Bing/SearchWeb/Web?Query=%27 +Xbox%27&$top=10&$format=JSON"; $account_key = "<my key>"; $mech->credentials($url, '', '', $account_key); $mech->get($url); print $mech->content();

Error: "The authorization type you provided is not supported. Only Basic and OAuth are supported". If I open the URL in browser, it is a Basic Pop Authentication with no Realm, the username should be blank and the password is the account key. I am able to view the JSON response. However, when I try to connect from the Perl Script it does not work due to authentication issues. I think my syntax for passing the credentials in the request is correct. Please let me know if there is a correction to be made to make it work.

Replies are listed 'Best First'.
Re: Bing API Authentication in Perl
by Anonymous Monk on Oct 03, 2012 at 02:32 UTC

    I think my syntax for passing the credentials in the request is correct.

    Doesn't look like it to me :)

    $mech->credentials( $username, $password ) Provide credentials to be used for HTTP Basic authentication for all sites and realms until further notice.

      I tried the method you have specified to use:

      $mech->credentials('',$account_key);

      however, it does not work either and gives the same error:

      "The authorization type you provided is not supported. Only Basic and OAuth are supported".

      I have even tried specifying a valid user agent, just in case they are performing User Agent Sniffing and it does not work.

      So, I set the user agent alias as follows:

      $mech->agent_alias('Windows IE 6');

      Again, I receive the same error for authentication.

Re: Bing API Authentication in Perl
by Mr. Muskrat (Canon) on Oct 03, 2012 at 15:37 UTC

    If you are going to use the four argument credentials, you need to pass the right data into it. Location is <host>:<port> as documented in LWP::UserAgent.

    Try this totally untested piece of code and see if you get better results.

    #!/usr/bin/perl use strict; use warnings; use WWW::Mechanize; use Crypt::SSLeay; my $mech = WWW::Mechanize->new(); # Don't pass the full URL as the location in the call to credentials! my $server = 'api.datamarket.azure.com'; my $url = "https://$server/Bing/SearchWeb/Web?Query=%27Xbox%27&$top=1 +0&$format=JSON"; my $loc = "$server:443"; my $account_key = "<my key>"; # Yes, WWW::Mech still supports LWP::UserAgent's four arg credentials $mech->credentials($loc, '', '', $account_key); $mech->get($url); print $mech->content();

    Update: After I posted this, I realized that you were using https so I updated the port from 80 to 443.

Re: Bing API Authentication in Perl
by zentara (Archbishop) on Oct 03, 2012 at 15:10 UTC
    If I open the URL in browser, it is a Basic Pop Authentication with no Realm, the username should be blank and the password is the account key.

    $mech->credentials($url, '', '', $account_key);

    From the looks of it, you are using a format for credentials that is used with a proxy, and you are stuffing the url you wish to retrieve in as the proxy url. Why not just use

    $mech->credentials( '', $account_key ); #??

    Also see Need help using WWW::Mechanize to access proxy and authentication for another way of setting up Basic Authorization.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Bing API Authentication in Perl
by Anonymous Monk on Oct 03, 2012 at 02:34 UTC

      The Bing::Search Module from CPAN is outdated (unless it has been updated without documentation of recent changes)

      If you read about that module on CPAN, it mentions that it makes use of the APPID (which was used with the previous Bing Search API 2.0 for developers).

      Now, they have moved to Azure Marketplace with the Account Keys replacing the AppIDs and also the URL which is used for the Search API has changed.

      I don't know if there is any module which would work with the new Bing Search API.