Loading...
Use any lib of you choice we are choosing the most popular lib called Retrofit
for this example.
To add bearer token in retrofit, you have to create a class that implements Interceptor
Tip:
Always remember to replace
API_SECRET
in the Bearer Token andproject-id
in the URL with the correct values from the Apito console.
If you are unsure where to find your API secrets and endpoints for your project, visit this page.
public class TokenInterceptor implements Interceptor{
@Override
public Response intercept(Chain chain) throws IOException {
//rewrite the request to add bearer token
Request newRequest=chain.request().newBuilder()
.header("Authorization","Bearer <API_SECRET>")
.build();
return chain.proceed(newRequest);
}
}
Now add your Interceptor class in OKHttpClient object and add that obejct in Retrofit object:
TokenInterceptor interceptor = new TokenInterceptor();
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor).
.build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl("https://api.apito.io/secured/rest/project-id")
.addConverterFactory(JacksonConverterFactory.create())
.build();