Problem with Basic Authentication on Search API

Answered Problem with Basic Authentication on Search API

  • Saturday, July 14, 2012 6:39 PM
     
     

    Hello,

    I have been unable to access the Bing Search and Bing Search - web only datasets I am subscribed to using 2 different account keys.  Using the PHP example in the Migration guide and after substituting the account key, I get the following:

    https://api.datamarket.azure.com/Bing/Search/Image?$format=json&Query=%27poland%27
    Warning:
    file_get_contents(https://api.datamarket.azure.com/Bing/Search/Image?$format=json&Query=%27poland%27) [function.file-get-contents]: failed to open stream: Connection refused in
    /home/cgerchma/public_html/test/test1.php on line 46 

    Line 46 = $response = file_get_contents($request, 0, $context);

    I have also tried pasting the following URL in my browser (IE 9), entering an account key in the password box and leaving the username blank:

    (https://api.datamarket.azure.com/Bing/Search/News?$format=JSON&Query=poland&Market=pl-PL)

    I get

    401 - Unauthorized: Access is denied due to invalid credentials.

    You do not have permission to view this directory or page using the credentials that you supplied

    Any help would be appreciated.

    Celine

All Replies

  • Sunday, July 15, 2012 2:51 AM
    Owner
     
     

    Could you verify that you are subscribed to the offer you are trying to use?

    Bing has two offers on the Marketplace (Search API and Search API Web only). We see some cases where folks subscribe to one, but try to access another one using the sample.

    Thanks,

    Max

  • Sunday, July 15, 2012 3:20 PM
     
     

    Hello Max,

    Under My Account/My data:

    Bing Search API (started on 7-13-2012) Subscription Active - 4989 Transactions remaining

    Bing Search API - Web Results Only (started on 7-14-2012) Subscription Active - 4999 Transaction remaining 

    When I click on Use, I am able to run queries which explains the remaining transactions balance but programmatically and via the browser, it fails as I mentioned above. 

    Thanks for your assistance.

    Celine

  • Sunday, July 15, 2012 6:58 PM
    Owner
     
     
  • Sunday, July 15, 2012 11:20 PM
     
     

    Hello again Max,

    I tried your corrected URL in the browser. The userid/pwd screen popped up 3 times: twice when I pasted the account key in password field so I finally manually typed it in and checked off to save credentials and obtained some query results. Unfortunately, the same does not hold true programatically - I continue to get the following error:

    Warning:
    file_get_contents(https://api.datamarket.azure.com/Data.ashx/Bing/Search/News?Query=%27poland%27&Market=%27pl-PL%27&$top=15&$format=JSON)
    [function.file-get-contents]: failed to
    open stream: Connection refused in /home/cgerchma/public_html/test/getnewslinks.php on line 19

    My code:

    <?
    error_reporting(E_ALL);
    include "../php/phplogin.php3";

    $ServiceRootURL = 'https://api.datamarket.azure.com/Data.ashx/Bing/Search/News?Query=%27poland%27&Market=%27pl-PL%27&$top=15&$format=JSON';

    echo $ServiceRootURL;

    $accountKey = 'MyPrimaryAccountKey';
    $context = stream_context_create(array(
                            'http' => array(
                                'proxy' => 'tcp://127.0.0.1:8888',
                                'request_fulluri' => true,
                                'header'  => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
                            )
                        ));

    // line 19 below
    $response = file_get_contents($ServiceRootURL,0,$context);
    $jsonobj = json_decode($response);

    //foreach($jsonobj->.....)
    // never gets to this point

    //}

    ?>

    Once again if you could shed some light on this, I'd would greatly appreciate it .

    Celine

  • Thursday, July 19, 2012 7:58 PM
     
     Answered

    Finally resolved this issue with the following:

    Replaced old $context code:

    $context = stream_context_create(array(
    'http' => array(
                                'proxy' => 'tcp://127.0.0.1:8888',
                                'request_fulluri' => true,
                                'header'  => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
                            )
                        ));

    with new $context code:

    $context = stream_context_create(array(
                            'http' => array(
                            'header'  => "Authorization: Basic " . base64_encode("ignored:".$accountKey)
                             )));

    Appears to working fine now after several query attempts.

    -Celine-

    • Marked As Answer by CelineG Thursday, July 19, 2012 7:58 PM
    •