java - Server returned HTTP response code: 400 for URL while fetching Tweets from twitter -


import java.io.bufferedreader; import java.io.dataoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.util.arraylist;  import javax.net.ssl.httpsurlconnection; import javax.xml.bind.datatypeconverter;  import org.slf4j.logger; import org.slf4j.loggerfactory;       public class twitter{     public static string gettwitterbearertokenurl()      {         return "https://api.twitter.com/oauth2/token";     }      public static void main(string[] args) throws ioexception {      final logger logger = loggerfactory.getlogger("twitter");      string base64key  = null;       string authorization = "authorization";     string bearer = "bearer";     string utf_charset = "utf-8";      logger.info("twitter::getbearertoken --> in");     string accesstoken = null;     stringbuffer buffer = new stringbuffer();       base64key = datatypeconverter                 .printbase64binary(("customer_id"+":"+"customer_secret")                         .getbytes());     logger.debug("base64key: " +base64key);      url twitterbearertokenurl;      try {             twitterbearertokenurl = new url(gettwitterbearertokenurl());          }catch (malformedurlexception e) {             e.printstacktrace();             logger.error("invalid request  " + e);             throw new runtimeexception("invalid request  " + e);         }      httpurlconnection urlconnection;     try{     urlconnection=       (httpurlconnection)twitterbearertokenurl.openconnection();      urlconnection.setrequestmethod("post");     urlconnection.setrequestproperty(authorization ,"basic "+ base64key);     urlconnection.setrequestproperty("content-type",                     "application/x-www-form-urlencoded;charset="                             + utf_charset);      urlconnection.setdooutput(true);      dataoutputstream wr = new dataoutputstream(                     urlconnection.getoutputstream());      wr.writebytes("grant_type=client_credentials");     wr.flush();     wr.close();      inputstream inputstr =  urlconnection.getinputstream();     inputstreamreader ireader = new inputstreamreader(inputstr);     bufferedreader reader1 =  new bufferedreader(ireader);      string read;      while ((read = reader1.readline()) != null) {      system.out.println(read);     buffer.append(read);     }           }catch (exception e) {             e.printstacktrace();             logger.error("unable connect twitter " + e);             throw new runtimeexception("unable connect twitter " + e);         }         accesstoken = buffer.tostring();          string[] tokens = accesstoken.split(",");          (string token : tokens) {             string[] parts = token.split(":");             if (parts.length != 2) {                 throw new runtimeexception("unexpected auth response");             } else {                 if (parts[0].equals("\"access_token\"")) {                     parts[1] = parts[1].replace("\"", "");                     parts[1] = parts[1].replace("}", "");                     accesstoken = parts[1];                     break;                 }             }         }          logger.debug("accesstoken: "+ accesstoken);         logger.info("twitter::getbearertoken --> out");           system.out.println("\naccess token : ----------> " + accesstoken+ "\n");         string username ="srbachchan";         int count = 10;          arraylist<string> tweets = new arraylist<string>();          httpsurlconnection geturlconnection;         geturlconnection = null;         url geturl = null;               string url2 = null;                 url2 = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name="                         + username +"&" + "count="+count;                  //url2 = url2.substring(0, (url2.length()-1));                 system.out.println("\nurl2 :--->" + url2+"\n");         try {               geturl = new url(url2);          } catch (malformedurlexception e) {             e.printstacktrace();             logger.error("invalid request  " + e);             throw new runtimeexception("invalid request  " + e);         }         try {             geturlconnection = (httpsurlconnection)geturl.openconnection();              geturlconnection.setrequestmethod("get");             geturlconnection.setrequestproperty(authorization, bearer                     + accesstoken);              geturlconnection.setrequestproperty("content-type",                     "application/x-www-form-urlencoded;charset="                             + utf_charset);              geturlconnection.setdooutput(true);          } catch (exception e) {              e.printstacktrace();              throw new runtimeexception("unable request process ");         }         dataoutputstream wr1 = new dataoutputstream(                 geturlconnection.getoutputstream());          wr1.writebytes("grant_type=client_credentials");         wr1.flush();         wr1.close();          system.out.println("*****************before buffer*****************");         bufferedreader reader;             try {              reader = new bufferedreader(new inputstreamreader(                     geturlconnection.getinputstream()));             string read;             while ((read = reader.readline()) != null) {                         buffer.append(read);             }              system.out.println(buffer.tostring());         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }     } } 

here error:

java.io.ioexception: server returned http response code: 400 url: https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=srbachchan&count=10     @ sun.net.www.protocol.http.httpurlconnection.getinputstream0(unknown source)     @ sun.net.www.protocol.http.httpurlconnection.getinputstream(unknown source)     @ sun.net.www.protocol.https.httpsurlconnectionimpl.getinputstream(unknown source)     @ com.aricent.oauth.twitter.main(twitter.java:206) 

context: trying fetch tweets of specific user twitter using access token obtained code throwing https: bad request 400. not able understand receiving access token when using fetch tweets throwing bad request exception.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -