Authorization header not recognized by Yelp Fusion API - java

I am passing an authorization header with my Retrofit GET requests as documented by the Yelp Fusion documentation, which is shown below:
Authorization: Bearer MY_API_KEY
I am adding a network interceptor in my onCreate() method as shown below:
OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();
okHttpBuilder.addInterceptor(new Interceptor() {
#Override
public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.addHeader("Authorization", "Bearer MY_API_KEY");
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
//Yelp Client init
yelpRetrofit = new Retrofit.Builder()
.baseUrl("https://httpbin.org/")
.client(okHttpBuilder.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
yelpClient = yelpRetrofit.create(YelpClient.class);
Here is my YelpClient class :
public interface YelpClient {
#GET("headers")
Call<YelpJSON> searchTest(); }
This is the response I get when sending the request:
{"error": {"code": "TOKEN_MISSING", "description": "An access token must be supplied in order to use this endpoint."}}
I have successfully retrieved a response from the Yelp Fusion API with Postman and curl both using identical parameters and headers as my Retrofit request, so I can't blame Yelp. I have also attempted statically and dynamically setting the header with no success. Can anyone help me figure out what is wrong here?

Related

how to get session cookie from server and set it into the api header?

There is cookie header in my api request, every time I have to copy this cookie from postman and paste in my code to make it work. how can I generate cookie in my app and give that value in cookie header?
this is my login code:
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("username","zee#earthonetechno.com")
.addFormDataPart("password","E1234567")
.build();
Request request = new Request.Builder()
.url("192.168.1.51/auth/login")
.method("POST", body)
.addHeader("User-Agent", "Koala Admin")
.addHeader("Content-Type", "application/json")
.addHeader("Cookie", "session=3e3710cb-9b41-47ea-ab1b-a1e1801e188b")
.build();
Response response = client.newCall(request).execute();
I want to put the cookie here in addHeader("Cookie", "session=3e3710cb-9b41-47ea-ab1b-a1e1801e188b")
I am developing in android studio
I found the solution for this
just include these lines in login part to get cookie
Headers allHeaders = response.headers();
headerValue = allHeaders.get("Set-Cookie");
and use this headerValue for subsequent api calls

How to pass parameters in a POST() request using the new HTTP client in Java

I just started using the new HTTP client in Java and I'm unsure on how to pass parameters for a PUT request.
The specific request I'm dealing with requires an Authentication token and a parameter type.
I've successfully dealt with the Authentication token using .headers()
I tried to do the same thing with the type parameter but I get an error message that states I haven't passed a type field.
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("...")) # The API url
.headers("Authorization", token, "type", "type 1")
.POST(HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = client.send(request,HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
As #ernest_k commented, we can pass parameters through by appending them to the end of the URL in this format: ?type=type1&param2=value2&param3=value3&param4=value4
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("..." + "?type=type 1"))
.headers("Authorization", token)
.POST(HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = client.send(request,HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());

AWS Chime: How to sign an HTTP Request for Create Meeting API in spring boot?

I didn't find any useful way to generate the Signature V4 in java for signing an AWS Chime HttpRequest. I want to use 3 apis of Aws Chime (Create meeting, create attendee and delete meeting) inside my java code.
Can anyone help me please !
API Details:
Url: https://service.chime.aws.amazon.com/meetings
Body: { "ClientRequestToken": "AXEXAMPLE", "MediaRegion":
"us-east-2" }
Headers: "content-type", "application/json" "host",
"service.chime.aws.amazon.com" "x-amz-date", "20200526T094404Z"
"authorization", ?(Generated Signature)
API Key: JHDCHGEXAEXAMPLE Secret Key : 4sjfkkjffs/sfkkh/sfkj/example
private Response createMeeting() {
OkHttpClient client = new OkHttpClient();
Response response = null;
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n\t\"ClientRequestToken\": \"AXEXAMPLE\",\n \"MediaRegion\": \"us-east-2\"\n}");
Request request = new Request.Builder()
.url("https://service.chime.aws.amazon.com/meetings")
.post(body)
.addHeader("content-type", "application/json")
.addHeader("host", "service.chime.aws.amazon.com")
.addHeader("content-length", "68")
.addHeader("x-amz-date", getIsoDate())
.addHeader("authorization", "?????????") //To be replaced by generated signature
.addHeader("cache-control", "no-cache")
.build();
try {
response = client.newCall(request).execute();
System.out.println("ResponseMessage===========> " + response.message());
} catch (IOException e) {
System.out.println("Error===========> " + e.getMessage());
}
return response;
}
The error I am getting is: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

Does Exoplayer supports playing videos via POST METHOD?

I have an API to retrieve videos from our server, the API use the POST method and needs Authorization for Headers and deviceInfo for body parameter.
example.
URL: https://myapi.com/api/pretty_video.mp4
BODY: deviceInfo = device info
HEADER: Authorization: Bearer "Token"
METHOD: POST
I can't find any example of ExoPlayer using POST method in playing videos from URL.
SOLVED!
I solved it by using OkHttp.
DefaultBandwidthMeter defaultBandwidthMeter = new DefaultBandwidthMeter();
DataSource.Factory dataSourceFactory = new OkHttpDataSourceFactory(new OkHttpClient
.Builder()
.connectTimeout(1, TimeUnit.MINUTES)
.readTimeout(1,TimeUnit.MINUTES)
.retryOnConnectionFailure(false)
.addInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
MediaType CONTENT_TYPE = MediaType.parse("application/x-www-form-urlencoded");
RequestBody requestBody = RequestBody.create(CONTENT_TYPE,"deviceinfo=12345");
Request request = chain.request().newBuilder()
.post(requestBody) . // HERE IS THE KEY
.addHeader("Accept", "application/json")
.addHeader("Authorization", "Bearer " + auth)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
return chain.proceed(request);
}
})
.build(), Util.getUserAgent(context,context.getString(R.string.app_name)), defaultBandwidthMeter);

Intercept query strings

I need to write a middleware for OKHttp to intercept all sended query parameters (key1=value1&key2=value2&...) and generate a digest according to the parameters and then put it on a specific header and send it along with the request, I can intercept all request through the following way:
OkHttpClient httpClient = new OkHttpClient();
httpClient.interceptors().add(new Interceptor() {
#Override
public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
Request original = chain.request();
String digest = "How can I get sended paramters?";
Request request = original.newBuilder()
.header("User-Agent", "Your-App-Name")
.header("Digest", digest)
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
But I can't find a way to retrieve the list of parameters! any ideas?
It's been a while, but I believe you can just do:
Request original = chain.request();
String params = original.url().query();
don't have an android environment to test this with at the moment though. If not look at the okhttp javadoc for Request and HttpUrl
EDIT
for the post body there's a question here which I think does what you're after, but in a nutshell it's something like:
Request original = chain.request();
Buffer buffer = new Buffer();
original.body().writeTo(buffer);
String bodyStr = buffer.readUtf8();

Categories