Bing Paid Web Results Only API Returns 403 Error

Answered Bing Paid Web Results Only API Returns 403 Error

  • Friday, December 07, 2012 8:05 PM
     
      Has Code

    Here is my code for doing a search using the Bing API.  We have the paid version for the Web Only API.  I have read somewhere else that someone else had an issue with the web results only API but surely there has to be someone out there who got this working.

    Here is my code that I use.  You will see from the error below the code that it fails at the last line of the code here and gives a 403 error.  Any help on this would be greatly appreciated.

    string keyterms = "";
    if (Request.QueryString["keywords"] != null) { keyterms = Request.QueryString["keywords"].Trim(); }
    
    var bingContainer = new Bing.BingSearchContainer(new Uri("https://api.datamarket.azure.com/Bing/Search"));
    var accountKey = "***";
    bingContainer.Credentials = new NetworkCredential(accountKey, accountKey);
    var query = bingContainer.Web(keyterms, null, null, null, null, null, null, null);
    var results = query.Execute();

    Here is the actual error that displays on the screen.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.Data.Services.Client.DataServiceClientException: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
    <title>403 - Forbidden: Access is denied.</title>
    <style type="text/css">
    <!--
    body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
    fieldset{padding:0 15px 10px 15px;} 
    h1{font-size:2.4em;margin:0;color:#FFF;}
    h2{font-size:1.7em;margin:0;color:#CC0000;} 
    h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
    #header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
    background-color:#555555;}
    #content{margin:0 0 0 2%;;}
    .content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;;}
    -->
    </style>
    </head>
    <body>
    <div id="header"><h1>Server Error</h1></div>
    <div id="content">
    <div class="content-container"><fieldset>
     <h2>403 - Forbidden: Access is denied.</h2>
     <h3>You do not have permission to view this directory or page using the credentials that you supplied.</h3>
    </fieldset></div>
    </div>
    </body>
    </html>
    
    
    Source Error:
    Line 39:             bingContainer.Credentials = new NetworkCredential(accountKey, accountKey);
    Line 40:             var query = bingContainer.Web(keyterms, null, null, null, null, null, null, null);
    Line 41:             var results = query.Execute();
    



All Replies

  • Sunday, December 09, 2012 12:30 AM
    Owner
     
     Answered

    Hi Dan,

    You are using the sample that is accessing the Bing Search API (not Bing Search Web API), which is causing the 403 error.

    The Search Web API end point is: https://api.datamarket.azure.com/Bing/SearchWeb while you reference https://api.datamarket.azure.com/Bing/Search in your code.

    The other suggestion: please make sure you remove or regenerate the account key you posted above for a pure security reasons :)

    Thanks,

    Max

    • Marked As Answer by Dan Kurszewski Monday, December 10, 2012 7:11 PM
    •  
  • Monday, December 10, 2012 3:45 PM
     
      Has Code

    Max,

    Thank you so much for your answer.  That definitely was the issue with it not working at all.  But now I am having a slightly different issue.

    If I use the code above and change the link to have SearchWeb in it, it does a global wide search which is expected.

    But if I change the keyterms to

    keyterms = keyterms + " site:www.mysite.com";

    in order to only search our site, then nothing ever comes back.  The searches are always returned as empty.

    Am I allowed to to searches on our site only with a Web Results Only search?  If so, can you see what I am doing wrong?

    Thanks,
    Dan

  • Monday, December 10, 2012 7:12 PM
     
     

    Max,

    Never mind my last message.  It turns out the load balancer was preventing the search from working on just the site.  So I changed it to www1.mysite.com and it worked.

    Thanks again for your help!!!

    Thanks,
    Dan

  • Monday, December 10, 2012 8:26 PM
    Owner
     
     

    Great to hear :)

    Thanks,

    Max