none
403 access denied error for bing search

    Question

  • //include config file
    var config = null;
    try {
    config = require('./config');
    }
    catch (e) {
    config = {};
    config.BingAPI = {};
    config.BingAPI.key = process.env.BING_API_KEY; 
    config.BingAPI.url = process.env.BING_API_URL; //'https://api.datamarket.azure.com/Bing/SearchWeb/v1/Image?$format=json&Query=';
    }

    //include express
    var express = require('express');
    //var app = express();
    var app = express.createServer(express.logger());
    app.use(express.bodyParser());

    //pull data from web
    var request = require("request");

    //open and read file
    var fs = require("fs");

    //templating
    var Plates = require("plates");



    //test a simple text output to make sure everything is working
    //access at http://localhost:3000/test in a browser
    app.get('/test', function (req, res) {
    res.send("Hello beautiful!");
    });


    //load the template which contains search form
    app.get('/', function (req, res) {
    fs.readFile('./templates/template-search.html', 'utf8', function (err, html) {
    if (err) {
    return console.log(err);
    }

    res.send(html);
    });
    });

    //template post-back, calls Bing API, loops through results and outputs
    app.post('/', function (req, res) {
    fs.readFile('./templates/template-search.html', 'utf8', function (err, html) {
    if (err) {
    return console.log(err);
    }

    var compile = "";
    var snip = "<p>(data)</p>";
    var searchTerm = req.param('txtSearchTerm', 'empty');


    GetSearchResults(searchTerm, function (jsonSearchResults) { 

    var jsonResults = JSON.parse(jsonSearchResults);
    var r = jsonResults.d.results;

    //var output = jsonResults.d.results[0].Description;

    for (var i = 0; i < r.length; i++) {
    //compile += snip.replace("(data)", i);
    compile += snip.replace('(data)', '<b><a href="' + r[i].Url + '">' + r[i].Title + '</a></b>'
    + '<br>' + r[i].Description
    + '<br><a href="' + r[i].Url + '">' + r[i].DisplayUrl + '</a>');
    }

    var compiled = { 'divResults': '<div class="row"><div class="span4">' + compile + '</div></div>' };
    var output = Plates.bind(html, compiled);

    res.send(output);
    });

    });
    });


    //call MS Web API
    GetSearchResults = function (searchTerm, callback) {

    var apiRoot = config.BingAPI.url;
    var apiKey = config.BingAPI.key;
    searchTerm = encodeURIComponent("'" + searchTerm + "'");
    var callURI = apiRoot + searchTerm;

    var options = {
    'method': 'GET',
    'uri': callURI,
    'headers': {
    'Authorization': 'Basic ' + new Buffer(apiKey + ':' + apiKey).toString('base64')
    }
    };
    console.log(callURI);
    request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
    //return body;
    callback(body);
    }
    else if (error) {
    console.log("error: " + error);
    //return 'error, check console log';
    callback('error, check console log');
    }
    else {
    console.log("Response code: " + response.statusCode + "\nContent: " + body);
    console.log("HHHH");
    //return 'error, check console log';
    callback('error, check console log');
    }
    });

    };



    //listen on port 3000 for localhost or whatever for heroku deploy
    var port = process.env.PORT || 3000;
    app.listen(port, function() {
      console.log("Listening on " + port);
    });

    and this is my config file

    var config = {};
    config.BingAPI = {};

    config.BingAPI.url = 'https://api.datamarket.azure.com/Bing/SearchWeb/v1/Web?$format=json&Query=';
    config.BingAPI.key = '********';

    module.exports = config;

    and my output is 

    Listening on 3000
    127.0.0.1 - - [Sun, 15 Dec 2013 15:17:56 GMT] "GET / HTTP/1.1" 200 1136 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0"
    https://api.datamarket.azure.com/Bing/SearchWeb/v1/Web?$format=json&Query='rose'
    Response code: 403
    Content: <!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>

    HHHH

    SyntaxError: Unexpected token e
        at Object.parse (native)
        at /home/adminuser/Downloads/Node.js-for-Bing-API-master/web.js:62:27
        at Request._callback (/home/adminuser/Downloads/Node.js-for-Bing-API-master/web.js:114:4)
        at Request.self.callback (/home/adminuser/Downloads/Node.js-for-Bing-API-master/node_modules/request/request.js:123:22)
        at Request.EventEmitter.emit (events.js:98:17)
        at Request.<anonymous> (/home/adminuser/Downloads/Node.js-for-Bing-API-master/node_modules/request/request.js:893:14)
        at Request.EventEmitter.emit (events.js:117:20)
        at IncomingMessage.<anonymous> (/home/adminuser/Downloads/Node.js-for-Bing-API-master/node_modules/request/request.js:844:12)
        at IncomingMessage.EventEmitter.emit (events.js:117:20)
        at _stream_readable.js:920:16

    Sunday, December 15, 2013 3:17 PM

Answers

All replies