java - Twitter call request -
i have problem source code twitter request. response returned twitter blank string. can advice can problem?
i have registered app on dev.twitter.com. called reach count.
this code brought tutorial: http://www.coderslexicon.com/demo-of-twitter-application-only-oauth-authentication-using-java/
package count_reach_twitter; //import com.sun.org.apache.xerces.internal.impl.dv.util.base64; import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.ioexception; import java.io.inputstreamreader; import java.io.outputstreamwriter; import java.io.unsupportedencodingexception; import java.net.malformedurlexception; import java.net.url; import java.net.urlencoder; import javax.net.ssl.httpsurlconnection; import org.apache.commons.codec.binary.base64; import org.json.jsonarray; import org.json.jsonobject; /** * * @author martin */ public class twittercall { // encodes consumer key , secret create basic authorization key private static string encodekeys(string consumerkey, string consumersecret) { try { string encodedconsumerkey = urlencoder.encode(consumerkey, "utf-8"); string encodedconsumersecret = urlencoder.encode(consumersecret, "utf-8"); string fullkey = encodedconsumerkey + ":" + encodedconsumersecret; byte[] encodedbytes = base64.encodebase64(fullkey.getbytes()); return new string(encodedbytes); } catch (unsupportedencodingexception e) { return new string(); } } // writes request connection private static boolean writerequest(httpsurlconnection connection, string textbody) { try { bufferedwriter wr = new bufferedwriter(new outputstreamwriter(connection.getoutputstream())); wr.write(textbody); wr.flush(); wr.close(); return true; } catch (ioexception e) { return false; } } // reads response given connection , returns string. private static string readresponse(httpsurlconnection connection) { try { stringbuilder str = new stringbuilder(); bufferedreader br = new bufferedreader(new inputstreamreader(connection.getinputstream())); string line = ""; while((line = br.readline()) != null) { str.append(line + system.getproperty("line.separator")); } return str.tostring(); } catch (ioexception e) { return new string(); } } // constructs request requesting bearer token , returns token string private static string requestbearertoken(string endpointurl) throws ioexception { httpsurlconnection connection = null; string encodedcredentials = encodekeys("my customer key","my customer secret key"); try { url url = new url(endpointurl); connection = (httpsurlconnection) url.openconnection(); connection.setdooutput(true); connection.setdoinput(true); connection.setrequestmethod("post"); connection.setrequestproperty("host", "api.twitter.com"); connection.setrequestproperty("user-agent", "reach count"); connection.setrequestproperty("authorization", "basic " + encodedcredentials); connection.setrequestproperty("content-type", "application/x-www-form-urlencoded;charset=utf-8"); connection.setrequestproperty("content-length", "29"); connection.setusecaches(false); //writerequest(connection, "grant_type=client_credentials"); // parse json response json mapped object fetch fields from. system.out.println("response: " + readresponse(connection)); system.out.println("end"); jsonobject obj = new jsonobject(readresponse(connection)); //(jsonobject)jsonvalue.parse(readresponse(connection)); //obj. if (obj != null) { string tokentype = (string)obj.get("token_type"); string token = (string)obj.get("access_token"); return ((tokentype.equals("bearer")) && (token != null)) ? token : ""; } return new string(); } catch (malformedurlexception e) { throw new ioexception("invalid endpoint url specified.", e); } { if (connection != null) { connection.disconnect(); } } } // fetches first tweet given user's timeline public static string fetchtimelinetweet(string endpointurl) throws ioexception { httpsurlconnection connection = null; try { url url = new url(endpointurl); connection = (httpsurlconnection) url.openconnection(); connection.setdooutput(true); connection.setdoinput(true); connection.setrequestmethod("get"); connection.setrequestproperty("host", "api.twitter.com"); connection.setrequestproperty("user-agent", "reach count"); connection.setrequestproperty("authorization", "bearer " + requestbearertoken("https://api.twitter.com/oauth2/token")); connection.setusecaches(false); // parse json response json mapped object fetch fields from. jsonarray obj = new jsonarray(readresponse(connection));//(jsonarray)jsonvalue.parse(readresponse(connection)); if (obj != null) { string tweet = ((jsonobject)obj.get(0)).get("text").tostring(); return (tweet != null) ? tweet : ""; } return new string(); } catch (malformedurlexception e) { throw new ioexception("invalid endpoint url specified.", e); } { if (connection != null) { connection.disconnect(); } } } }
main function calling next method:
system.out.println(twittercall.fetchtimelinetweet("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=dj_fedy&count=50"));
thank you
i'm not sure why code gets 403 twitter. can think of 2 things:
- according bottom of application-only authentication doc,
403
response given when use bearer token on endpoint doesn't support application-only auth. cannot find whetherget on statuses/user_timeline
allowed application-only auth. - according get statuses/user_timeline doc, can request tweets protected user when authenticated user either "owns" timeline or approved follower of owner. i'm not sure whether case you.
Comments
Post a Comment