search api of bing azure marketpalce with java -


tring use search api of bing azure marketpalce java have code :

import org.apache.commons.codec.binary.base64; import org.apache.http.client.responsehandler; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.basicresponsehandler; import org.apache.http.impl.client.defaulthttpclient;  public class bingapi2 {     public static void main(string[] args) throws exception{         bingapi2 b = null;         b.getbing();      }  public static void getbing() throws exception {          httpclient httpclient = new defaulthttpclient();          try {             string accountkey = "myaccountkey=";             byte[] accountkeybytes = base64.encodebase64((":" + accountkey).getbytes());             string accountkeyenc = new string(accountkeybytes);              httpget httpget = new httpget("https://api.datamarket.azure.com/data.ashx/bing/search/web?$query=%27datamarket%27&$format=json");             httpget.setheader("authorization", "basic <"+accountkeyenc+">");              system.out.println("executing request " + httpget.geturi());              // create response handler             responsehandler<string> responsehandler = new basicresponsehandler();             string responsebody = httpclient.execute(httpget, responsehandler);             system.out.println("----------------------------------------");             system.out.println(responsebody);             system.out.println("----------------------------------------");          } {             // when httpclient instance no longer needed,             // shut down connection manager ensure             // immediate deallocation of system resources             httpclient.getconnectionmanager().shutdown();         }     }  } 

i error :

exception in thread "main" org.apache.http.client.httpresponseexception: authorization type provided not supported. basic , oauth supported

first thing see line

byte[] accountkeybytes = base64.encodebase64((":" + accountkey).getbytes());

should read :

byte[] accountkeybytes = base64.encodebase64((accountkey + ":" + accountkey).getbytes());

also there reason you're using apache libraries this? code use getting json objects bing uses java.net , looks this:

import java.net.urlconnection; import java.net.url; import java.io.inputstreamreader;  class bingjson{    jsonobject getjsonfrombing(string term){   try{     urlconnection c = new url(term).openconnection();     string key = (datatypeconverter.printbase64binary(("xxx" + ":" + "xxx").getbytes("utf-8")));     c.setrequestproperty("authorization", string.format("basic %s",key));     c.connect();     //etc.   } } 

to build json object follow code: convert inputstream jsonobject


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -