Dotaz Bing API Authorization not working

  • Thursday, August 16, 2012 2:49 PM
     
      Has Code

    I recently got an email from Microsoft saying that the Bing API was moving to the Windows Azure Marketplace. It seemed that the main difference between the new request was the authentication.

    After reading many posts on forums, I found this:

    $accountKey = '########';
    	$api =  'https://api.datamarket.azure.com/Bing/Search/Web?$format=json&Query=';
    	$context = stream_context_create(array(
    		'http' => array(
    			'request_fulluri' => true,
    			'header'  => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
    		)
    	));
    	$request = $api.'%27'.$q.'%27';
    	$result = file_get_contents($request, 0, $context);

    However, I still get the error "The authorization type you provided is not supported. Only Basic and OAuth are supported".

    Does anyone know how I can fix this. I have also tried cURL and that doesn't work.
    Thanks to anyone who can find me a solution.

All Replies

  • Friday, August 17, 2012 3:49 AM
    Moderator
     
     

    Hi,

    I suggest you refer to another similar topic thread:

    http://social.msdn.microsoft.com/Forums/en-US/DataMarket/thread/10e766f6-98c7-4a1d-ae3b-1efca05deebc

    Hope this helps.


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework

  • Friday, August 17, 2012 8:53 AM
     
     
    Thanks for that link. I looked through it and it only shows the request being done through Javascript/jQuery. At the moment, I'm currently using PHP. Do you know if there's a way of fixing this using PHP and not jQuery?
  • Friday, August 17, 2012 9:53 AM
    Moderator
     
      Has Code

    Try this code:

    <?php            
                    if (isset($_POST['submit'])) 
                    {
                        // Replace this value with your account key
                        $accountKey = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=';
                
                        $ServiceRootURL =  ‘https://api.datamarket.azure.com/Bing/Search/';
                        
                        $WebSearchURL = $ServiceRootURL . 'Image?$format=json&Query=';
                        
                        $context = stream_context_create(array(
                            'http' => array(
                                'proxy' => 'tcp://127.0.0.1:8888',
                                'request_fulluri' => true,
                                'header'  => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
                            )
                        ));
    
                        $request = $WebSearchURL . urlencode( '\'' . $_POST["searchText"] . '\'');
                        
                        echo($request);
                        
                        $response = file_get_contents($request, 0, $context);
                        
                        $jsonobj = json_decode($response);
                        
                        echo('<ul ID="resultList">');
     
                        foreach($jsonobj->d->results as $value)
                        {                        
                            echo('<li class="resultlistitem"><a href="' . $value->MediaURL . '">');
                            
                            echo('<img src="' . $value->Thumbnail->MediaUrl. '"></li>');
                        }
                        
                        echo("</ul>");
                    } 
                ?>

    And get more info from this file:

    http://www.google.com/url?sa=t&rct=j&q=marketplace%20bing%20api%20with%20php&source=web&cd=2&ved=0CFEQFjAB&url=http%3A%2F%2Fwww.bing.com%2Fwebmaster%2Fcontent%2Fdevelopers%2FADM_MIGRATION_GUIDE.docx&ei=E2T2T9zaEIS0rAfQvLzUBg&usg=AFQjCNGb51AqhxLDxCqHgUDymDuoNYFzVQ&cad=rja

    Hope this helps.


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework

  • Friday, August 17, 2012 10:02 AM
     
      Has Code

    OK, so I have tried your code. The code I was using before I got from that Migration Guide (the same link you sent me), and it seems that the only difference between my previous code and the code you posted, was the this line 

    'proxy' => 'tcp://127.0.0.1:8888'

    I now no longer get the same error but get this instead:

    Warning: file_get_contents() [function.file-get-contents]: Couldn't connect to server in/home/public_html/getResults.php on line 17

    Warning: file_get_contents(https://api.datamarket.azure.com/Bing/Search/Web?$format=json&$top=8&Query=%27xbox%27&$skip=0) [function.file-get-contents]: failed to open stream: operation failed in/home/public_html/getResults.php on line 17


    • Edited by JasonLipo Friday, August 17, 2012 10:03 AM
    •