Authorization for Spotify API in Java HttpURLConnection - java

I'm relatively new to using http and APIs but I was trying to use HttpURLConnection in Java to connect to the Spotify API. I managed to get a GET to work but I can't figure out how to make the authorization work in order to access other materials. Here's my code, can anyone see what I'm doing wrong? It's returning a 400 response code.
URL url = new URL("https://accounts.spotify.com/api/token?grant_type=client_credentials");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Authorization", Base64.getEncoder().encodeToString("Basic bdfc603f24c54078a7365d3af39c2aed:<ClientSecret>".getBytes()));

You're missing the Content-Type, see my example.
URL url = new URL("https://accounts.spotify.com/api/token?grant_type=client_credentials");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("User-Agent", "Mozilla/5.0");
conn.setRequestProperty("Authorization", "Basic MDg4ZDc=");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

Related

How can I send POST parameters with HttpURLConnection POST in java?

I am not familiar with https requests so please take it easy on me.
I want to make a post call and retrieve a token for a url. The url is something like:
/auth/token?param1=value1&param2=value2&param3=value3&param4={{value4}}&param5={{value5}}
I make the post
HttpURLConnection conn = (HttpURLConnection) authentication.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestMethod("POST");
but in order to get the response I have to send the parameters.
I've tried to write the url params as string to the connection outputstream but it doesnt work.
Any help is appreciated.

How can I authorize Google Scope for a DELETE HTTP request on Java?

I'm trying to DELETE some emails from my organization. I have this code.
URL url = new URL("https://www.googleapis.com/admin/directory/v1/users/userKey");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestProperty(
"Content-Type", "application/x-www-form-urlencoded" );
httpCon.setRequestMethod("DELETE");
httpCon.connect();
But I know that its missing the authorization that ask Google. Acording to this page: https://developers.google.com/admin-sdk/directory/v1/reference/users/delete
I don't know how to make that happen,
I'm new on this, can you help me please?

HttpUrlConnection PATCH request using Java

I am trying to do a http PATCH request but I always get the 404 error, so maybe the settings of my connection are not correct:
URL url = new URL("MyPath");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("X-HTTP-Method-Override", "PATCH");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestMethod("POST");
JsonObject jo = createMyJson();
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(jo.toString());
out.close();
System.out.println(conn.getResponseCode());
System.out.println(conn.getResponseMessage());
I get the 404 error, Not found. When doing the same request using Postman, this is working..
Thank you for your help.
Not all servers support X-HTTP-Method-Override. In that case your last resort is (if you are not using a decent HTTP client) to hack the URLConnection object.
I posted a complete solution here on SO, check it out.

When HTTPConnection is performed and what is its status

I have below code which generate OutputStream from HttpURLConnection
When connection is actually performed and how can I check its status?
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
httpCon.addRequestProperty("X-Auth-Token", getAuthToken());
httpCon.setDoInput(true);
httpCon.setRequestProperty("Connection", "close");
httpCon.setReadTimeout(READ_TIMEOUT);
httpCon.setRequestProperty("Transfer-Encoding","chunked");
httpCon.setDoOutput(true);
httpCon.setChunkedStreamingMode(STREAMING_CHUNK);
mOutputStream = httpCon.getOutputStream();
The underlying TCP connection is created (or allocated from a connection pool) when you get one of the streams or the response code, or call connect(). The HttpURLConnection object itself isn't a TCP connection.

Failing to connect to a web server with Java

I got a strange task: to connect to webserver using Java even though I'm a php programmer. I used to program Java back at the university but it's been good few years since.
I took a code sample from stack-overflow. It runs fine, no compilation errors no exceptions, but, I don't see any outgoing connection when I sniff with Fiddler, and my server side script doesn't see any incoming connections either.
Any ideas of what can be wrong? Or, how do I debug this situation?
String urlParameters = "param1=a&param2=b&param3=c";
String request = "http://example.com/index.php";
URL url = new URL(request);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches (false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
connection.disconnect();

Categories

Resources