Saturday 3 May 2014

creating bing search api apps







Signup with your Microsoft account
After signup it will ask for subscription for search
 Then click on my account tab it will ask some details  just fill it

By using this primary account key we will get the details

Then go to developers tab on left side
Just go through register button as I already registered I have one application just click on register button

In redirect url just place your local visual studio or eclipse ide url  from browser other  wise  it won’t work

Just copy below code and replace with your primary account key


<html>
<head>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.js" type="text/javascript"></script>
    <script type="text/javascript">
                   
        var accountKey = "XxY4qvgYloTKfZ3DsKfW3BFc1a1NenIuiSisHgkTOwY";///your account primary key
        var accountKeyEncoded = base64_encode(":" + accountKey);

        jQuery.support.cors = true;

        function setHeader(xhr) {
            xhr.setRequestHeader('Authorization', "Basic " + accountKeyEncoded);
            //'Basic <Your Azure Marketplace Key(Remember add colon character at before the key, then use Base 64 encode it');
        }

        function GetBing() {
            //Build up the URL for the request
            var requestStr = "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query=%27xbox%27&$top=50&$format=json";

            //Return the promise from making an XMLHttpRequest to the server
            $.ajax({
                url: requestStr,
                beforeSend: setHeader,
                context: this,
                type: 'GET',
                success: function (data, status) {
                    var results = data;
                    var imgSrc = data.d.results[0].MediaUrl;
                    var imgElement = document.getElementById("theImage");
                    imgElement.src = imgSrc;
                    //       imgElement.width = 200;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        }

        function base64_encode(data) {
          
            var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
            var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
              ac = 0,
              enc = "",
              tmp_arr = [];

            if (!data) {
                return data;
            }

            do { // pack three octets into four hexets
                o1 = data.charCodeAt(i++);
                o2 = data.charCodeAt(i++);
                o3 = data.charCodeAt(i++);

                bits = o1 << 16 | o2 << 8 | o3;

                h1 = bits >> 18 & 0x3f;
                h2 = bits >> 12 & 0x3f;
                h3 = bits >> 6 & 0x3f;
                h4 = bits & 0x3f;

                // use hexets to index into b64, and append result to encoded string
                tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
            } while (i < data.length);

            enc = tmp_arr.join('');

            var r = data.length % 3;

            return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);

        }

    </script>
</head>

<body onload="GetBing();">
    <img id="theImage" />
</body>
</html> 
You will see below output

As I searched for xobox image so it displayed xbox image
This is method with javascript
You can  follow this flexible application you can get idea by going to this link http://msdn.microsoft.com/en-us/library/gg312154.aspx

1 comment: