I am trying to Authenticate Oauth 1.0 in android.
I send a request with postman and get a response via postman and this program gives me a Java code but it doesn't work.its return 401 error
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://test.com/wp-json/wc/v3/customers?
oauth_consumer_key=KEY&oauth_token=&oauth_signature_method=HMAC-HA1&oauth_timestamp=1564471240&oauth_nonce=iEo45PESRdt&oauth_version=1.0&oauth_signature=NQR4Xr5OKlb3H+rL0y2PNLdfXpY=")
.get()
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("User-Agent", "PostmanRuntime/7.15.2")
.addHeader("Accept", "*/*")
.addHeader("Cache-Control", "no-cache")
.addHeader("Postman-Token", "6fb2e16e-376c-453a-bf30-18ef6c00020c,c14db88a-55dd-4287-a132-8ca441a5e3f0")
.addHeader("Host", "test.com")
.addHeader("Accept-Encoding", "gzip, deflate")
.addHeader("Connection", "keep-alive")
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();
Seems like code generated by postman does not respect Authentication section.
Go to Headers section and click "9 hidden" to see the "Authorization" header
show hidden
see the auth header
Related
I have a nodejs web server running and I want to a body to the request in my java program.
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.GET()
.header( "Content-Type", "text/plain;charset=UTF-8")
.uri(URI.create("http://localhost:3000"))
.POST(HttpRequest.BodyPublishers.ofString("Hello"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("status:" + response.statusCode());
System.out.println("response:" + response.body());
If I delete this line: .POST(HttpRequest.BodyPublishers.ofString("Hello")) everything works fine, I get the response from the server, but there is no body to the request.
With this line I get status code 404 and the body of the response to the client is:
How can I add body to the request?
First of all try understand differences between GET and POST HTTP methods
https://www.w3schools.com/tags/ref_httpmethods.asp
There is also a good example in docs: https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpRequest.BodyPublishers.html
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://example.com/"))
.header("Content-Type", "text/plain; charset=UTF-8")
.POST(BodyPublishers.ofString("some body text"))
.build();
You can also look here as well
https://www.baeldung.com/java-9-http-client
I am trying to retrieve my user profile from graph.microsoft as show here. I am using a Java library OKHttp to achieve this however the server is returning special characters in the response. I checked my headers and I did include "Accept-Encoding: gzip". However the issue is not resolved. See code under;
Java Code
Request userProfileRequest = new Request.Builder()
.url("https://graph.microsoft.com/v1.0/me")
.get()
.addHeader("Authorization", "Bearer "+accessTkn)
.addHeader("Accept", "*/*")
.addHeader("Cache-Control", "no-cache")
.addHeader("Content-Type", "application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8")
.addHeader("Accept-Encoding", "gzip")
.addHeader("Connection", "keep-alive")
.addHeader("cache-control", "no-cache")
.build();
Response userProfileResponse = client2.newCall(userProfileRequest).execute();
System.out.println("Authorization is " +userProfileRequest.header("Authorization"));
System.out.println(userProfileResponse.body().string());
Console output
OkHttp does transparent compression for you. However by explicitly specifying "Accept-Encoding: gzip" you are indicating that you want gzip compression and will handle it yourself.
Removing everything except Authorization as you have done in your answer is the correct solution.
The solution that worked for me was to remove all the headers except for "Authorization"
Java Code
Request userProfileRequest = new Request.Builder()
.url("https://graph.microsoft.com/v1.0/me")
.get()
.addHeader("Authorization", "Bearer "+accessTkn)
.build();
I am calling basic auth rest api. seperate java program is working properly but when I add it into Grails application it throws exception saying
errors.GrailsExceptionResolver - SocketException occurred when processing request: [GET]
Message: Connection reset
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://....")
.get()
.addHeader("Authorization", "Basic Q...")
.addHeader("cache-control", "no-cache")
.addHeader("Postman-Token", "77768b44-678d-4803-8ea2-46767192b161")
.build();
Response response = client.newCall(request).execute();
I am trying to access Mindsphere URL with Java Code. I am getting 403 forbidden error while doing it. While I am able to hit other POST URL's for other sites, Mindsphere URL is getting blocked by same piece of Java Code. Can someone help?
What am i missing in my Code?
restTemplate.exchange(,,*,TimeseriesData.class) is line giving error
MindSphere demands a authorization header with a JWT Token, if you call directly the API. I guess you have an Developer account in MindSphere. Try Application credentials in the Developer cockpit. With that credentials you can get a bearer token with an oauth flow.
If not just ping me again.
See Exampel with OK HTTP
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials");
Request request = new Request.Builder()
.url("https://questdev.piam.eu1.mindsphere.io/oauth/token")
.post(body)
.addHeader("Accept", "application/json")
.addHeader("cache-control", "no-cache,no-cache")
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("Postman-Token", "24126d6b-3461-48fb-9060-6fd005804227")
.build();
Response response = client.newCall(request).execute();
I have another one for logging in. So this is how I see it, every time I try running this code, it asks me to authenticate.
So this is how it works I suppose, I basically have to run the login code first, store the cookies sent in response by the server and use it for subsequent operations, like for example making changes in the servers database.
I am able to make these changes via postman.
so how do i receive the cookies sent in response by the server?
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("multipart/form-data; boundary=----
WebKitFormBoundary7MA4YWxkTrZu0gW");
RequestBody body = RequestBody.create(mediaType, "------
WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
name=\"username\"\r\n\r\nadmin\r\n------
WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
name=\"password\"\r\n\r\nadmin\r\n------
WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
name=\"this_is_the_login_form\"\r\n\r\n1\r\n------
WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
name=\"post_data\"\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");
Request request = new Request.Builder()
.url("http://localhost:8081/iclock/accounts/login/?
next=/iclock/data/iclock/")
.post(body)
.addHeader("content-type", "multipart/form-data; boundary=----
WebKitFormBoundary7MA4YWxkTrZu0gW")
.addHeader("Cache-Control", "no-cache")
.addHeader("Postman-Token", "86439550-93f7-6d60-1aab-7d289c137b0d")
.build();
Response response = client.newCall(request).execute();
response.headers("Set-Cookie") returns the cookie sent by the server in a String format. Refer this for more information related to the Response class in OkHttp.