How to send Bearer token with request - java

I'm attempting to use the Twitter API to return a List of tweets and following this tutorial to invoke a GET request with parameters: https://www.baeldung.com/java-http-request
Here is my code based off tutorial:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class GetTweets {
public static void main(String argsp[]) throws IOException {
URL url = new URL("https://api.twitter.com/2/users/1315617658461659136/tweets");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
Map<String, String> parameters = new HashMap<>();
parameters.put("Token", "******");
con.setDoOutput(true);
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(ParameterStringBuilder.getParamsString(parameters));
out.flush();
out.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
System.out.println("Response is "+content);
}
}
returns:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 405 for URL: https://api.twitter.com/2/users/1315617658461659136/tweets
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1919)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:250)
at com.reactive.api.twitter.GetTweets.main(GetTweets.java:27)
If I use Postman and set the Bearer token in the Authorization tab the tweets are returned correctly :
So it seems I'm not passing the Bearer token parameter correctly ?
How to pass the Bearer token with the Get request ?

The bearer goes in the "Authorization" header:
con.setRequestProperty("Authorization", "Bearer " + token);

Related

Calling GET API in java using bearer token

I need to send bearer token for the below code. Below code is working but I have to call another rest end point with GET which is protected by token. What addition I need to do in below code? I just want to replace url and need to add bearer token.
package com.shruti.getapi;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
public class NetClientGet {
public static void main(String[] args) {
try
{
System.out.println("Inside the main function");
URL weburl=new URL("http://dummy.restapiexample.com/api/v1/employees");
HttpURLConnection conn
= (HttpURLConnection) weburl.openConnection(Proxy.NO_PROXY);
//HttpURLConnection conn = (HttpURLConnection) weburl.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
System.out.println("Output is: "+conn.getResponseCode());
System.out.println("Output is: ");
System.setProperty("http.proxyHost", null);
//conn.setConnectTimeout(60000);
if(conn.getResponseCode()!=200)
{
System.out.println(conn.getResponseCode());
throw new RuntimeException("Failed : HTTP Error Code: "+conn.getResponseCode());
}
System.out.println("After the 2 call ");
InputStreamReader in=new InputStreamReader(conn.getInputStream());
BufferedReader br =new BufferedReader(in);
String output;
while((output=br.readLine())!=null)
{
System.out.println(output);
}
conn.disconnect();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
conn.setRequestProperty("Accept", "application/json");
You want this idea, but for the Authorization header.
Authorization = credentials
RFC 6750 includes an ABNF description of the credentials for an OAuth2 Bearer Token
b64token = 1*( ALPHA / DIGIT /
"-" / "." / "_" / "~" / "+" / "/" ) *"="
credentials = "Bearer" 1*SP b64token

Trying to HTTP POST But Getting MalformedURLException: no protocol: yahoo.com

Overall I'm trying to write a script that captures the servers' response to an HTTP POST using java.
Unfortunately, I'm stuck at encoding the URL portion of it. While I followed several online example on encoding a URL, I still get MalformedURLException...
Any idea what might go wrong in the encoding process?
The error:
$ java client_post
Sending Http POST request
Exception in thread "Main Thread" java.net.MalformedURLException: no
protocol: http%3A%2F%2Fyahoo.com
at java.net.URL.<init>(URL.java:567)
at java.net.URL.<init>(URL.java:465)
at java.net.URL.<init>(URL.java:414)
at client_post.sendPost(client_post.java:30)
at client_post.main(client_post.java:23)
The code:
//package client_post;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class client_post {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
client_post http = new client_post();
System.out.println("\nSending Http POST request");
http.sendPost();
}
// HTTP POST request
private void sendPost() throws Exception {
//String url =<host:port/create/service>
String url = "http://yahoo.com";
String EncoderUrl = URLEncoder.encode(url, "UTF-8");
URL obj = new URL(EncoderUrl);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language","en-US,en;q=0.5");
String urlParameters = "<string base64>";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
System.out.println(response.toString());
}
}
When you are encoding url your url becomes like below
http%3A%2F%2Fyahoo.com
Dont encode untill you have something special in it.
Your programm is also throwing class cast exception
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
Above should be like below
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
Below is working programm.
package com.ds.portlet.library;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class client_post {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
client_post http = new client_post();
System.out.println("\nSending Http POST request");
http.sendPost();
}
// HTTP POST request
private void sendPost() throws Exception {
//String url =<host:port/create/service>
String url = "http://yahoo.com";
// String EncoderUrl = URLEncoder.encode(url, "UTF-8");
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language","en-US,en;q=0.5");
String urlParameters = "<string base64>";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
System.out.println(response.toString());
}
}
It looks like you're trying to encode the entire URL, including the :// and similar characters. The purpose of URL encoding is to hide those characters in a path or query part, and they shouldn't be encoded in the main URL. Use URLEncoder only for parameters or application/x-www-form-urlencoded contents.

HttpURLConnection GET call with parameters not working

I have a very simple code which is not working.
Class HttoCon:
package test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class HttpCon {
public static void main(String[] args) {
try {
sendGet();
} catch (Exception e) {
e.printStackTrace();
}
}
// HTTP GET request
private static void sendGet() throws Exception {
String url = "http://myweb.com/public/resource/data/stream";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("begin", "1430295300000");
con.setRequestProperty("end", "1430297279988");
con.setRequestProperty("id", "140621");
con.setRequestProperty("time", "FIFTEEN_MINUTE");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
}
Error Trace:
{"fault":{"exceptionType":"MissingServletRequestParameterException","host":"12.205.101.123","cause":"Required Long parameter 'id' is not present","status":"400","query":"/public/resource/data/stream","stackTrace":"org.springframework.web.bind.MissingServletRequestParameterException: Required Long parameter 'id' is not present\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.raiseMissingParameterException(AnnotationMethodHandlerAdapter.java:774)\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestParam(HandlerMethodInvoker.java:509)\n\tat
NOTE:
If I hit the url directly in browser it works fine.
http://myweb.com/public/resource/data/stream?begin=1430295300000&end=1430297279988&id=140621&time=FIFTEEN_MINUTE
UPDATE:
Using curl:
curl -X GET http://myweb.com/public/resource/data/stream?begin=1430295300000&end=1430297279988&id=140621&time=FIFTEEN_MINUTE
Above curl doesn't work and the exception is same -
curl -X GET 'http://myweb.com/public/resource/data/stream?begin=1430295300000&end=1430297279988&id=140621&time=FIFTEEN_MINUTE'
This curl works fine.
You create a String containing the parameters, which then gets appended onto the URL. Create a URL Obj from this. For example :-
String this.url = "http://myweb.com/public/resource/data/stream";
this.url += "?begin=1430295300000&end=1430297279988&id=140621
&time=FIFTEEN_MINUTE";
this.urlObj = new URL(this.url);
Then you create the connection as in your original example.
openConnection will establish the connection and issue the GET. Subsequent settings of GET parameters on the returned URLConnection object are ignored, since the URL has already been open.
Add the parameters to the URL as query string params just as the link above and open that link
or
try to post the parameters to the server (which is even better)

Java Server returned HTTP response code: 401

Hi I write java program to do a http post request by Http Basic Authentication
but it always shows error 401. My username and password is right can login the website. I don't know where wrong?
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import sun.misc.*;
import javax.net.ssl.HttpsURLConnection;
import org.apache.commons.codec.*;
#SuppressWarnings("unused")
public class hello {
/**
* #param args
*/
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
hello http = new hello();
//System.out.println("Testing 1 - Send Http GET request");
//http.sendGet();
System.out.println("\nTesting 2 - Send Http POST request");
http.sendPost();
}
#SuppressWarnings("unused")
private void sendPost() throws Exception {
String url = "https://mds.datacite.org/doi";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
String userPassword= "username:password";
String encoding = new String(org.apache.commons.codec.binary.Base64.encodeBase64(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(userPassword)));
System.out.println(encoding);
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "text/plain");
con.setRequestProperty("charset", "UTF-8");
con.setRequestProperty("Authorization","Basic"+encoding);
String urlParameters = "doi=xxxxxx&url=http://xxxxx/dataset/1xxx099";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
401 == Unauthorized, which means your username/password combo are incorrect
you need a space after "Basic" in the Authorization header

Urban Airship Push code

I've followed the UA tutorials and got my APID , and successfully recieved test push message on my android device.
Now the next thing that I like to do is to target my device using JAVA and send push messaged.
From what I've seen so far the best way to achieve this is using their web API.
However When I'm trying to send a post message I always get the following error :
java.io.IOException: Server returned HTTP response code: 401 for URL: https://go.urbanairship.com/api/push/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at com.Sender.Sender.main(Sender.java:56)
This is the code that I use :
package com.Sender;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Sender {
/**
* #param args
*/
public static void main(String[] args) {
try {
String responseString = "";
String outputString = "";
String username = "MWMoRVhmRXOG6IrvhMm-BA";
String password = "ebsJS2iXR5aMJcOKe4rCcA";
MyAuthenticator ma= new MyAuthenticator(username, password);
Authenticator.setDefault(ma);
URL url = new URL("https://go.urbanairship.com/api/push/");
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) urlConnection;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
String APID = "23eef280-19c8-40fc-b798-788f50e255a2";
String postdata = "{\"android\": {\"alert\": \"Hello from JAVA!\"}, \"apids\": [\""+APID+"\"]}";
byte[] buffer = new byte[postdata.length()];
buffer = postdata.getBytes("UTF8");
bout.write(buffer);
byte[] b = bout.toByteArray();
httpConn.setRequestProperty("Content-Length",
String.valueOf(b.length));
httpConn.setRequestProperty("Content-Type", "application/json");
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
out.write(b);
out.close();
InputStreamReader isr = new InputStreamReader(
httpConn.getInputStream());
BufferedReader in = new BufferedReader(isr);
while ((responseString = in.readLine()) != null) {
outputString = outputString + responseString;
}
System.out.println(outputString);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
Can you help me please ?
HTTP 401 represent Unauthorized access. In your code even though your created Authenticator, you didn't provide it as part of post request header. Here is tutorial on how to set authenticator for a URLConnection.

Categories

Resources