401 Error Sending GET users/show to Twitter API -
i attempted user details of twitter user has authorized app using users/show. using raw java this, not twitter api (hence lack of documentation). able request token, , access token 401 error on last step when attempting user info. notice doing wrong here?
int t = (int) ((system.currenttimemillis()) / 1000); string n = generatenonce(); string parameterstr = "oauth_consumer_key=" + twitter_key + "&oauth_nonce=" + n + "&oauth_signature_method=" + oauthsignaturemethod + "&oauth_timestamp=" + t + "&oauth_token=" + user_token + "&oauth_version=1.0" + "&screen_name=" + user_screen_name; string oauthsignature = ""; string authorizationheaderstr = ""; try { string signaturebasestr = "get&https%3a%2f%2fapi.twitter.com%2f1.1%2fusers%2fshow.json&" + urlencoder.encode(parameterstr, "utf-8"); oauthsignature = urlencoder.encode(computesignature(signaturebasestr, twitter_secret + "&" + user_secret_token), "utf-8"); authorizationheaderstr = "oauth oauth_consumer_key=\"" + twitter_key + "\",oauth_nonce=\"" + n + "\",oauth_signature=\"" + oauthsignature + "\",oauth_signature_method=\"hmac-sha1\",oauth_timestamp=\"" + t + "\",oauth_token=\"" + user_token + "\",oauth_version=\"1.0\""; system.out.println(signaturebasestr); system.out.println(authorizationheaderstr); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (generalsecurityexception e) { e.printstacktrace(); } //create post , receive oauth token try { string urlstr = "https://api.twitter.com/1.1/users/show.json?" + "screen_name=" + user_screen_name; url url = new url(urlstr); httpsurlconnection conn = (httpsurlconnection) url.openconnection(); conn.setrequestmethod("get"); conn.setrequestproperty("authorization", authorizationheaderstr); //send post conn.setdooutput(true); dataoutputstream wr = new dataoutputstream(conn.getoutputstream()); wr.flush(); wr.close(); //get post int twitterresponsecode = conn.getresponsecode(); bufferedreader in = new bufferedreader(new inputstreamreader(conn.getinputstream())); string inputline; stringbuffer twitterresponse = new stringbuffer(); while((inputline = in.readline()) != null) { twitterresponse.append(inputline); } in.close(); system.out.println(twitterresponse); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); }
the issue outputstream:
dataoutputstream wr = new dataoutputstream(conn.getoutputstream()); wr.flush(); wr.close(); i removed , working fine. i'm not sure why there begin with.
Comments
Post a Comment