Answer the question
In order to leave comments, you need to log in
How, for example, to work with api vkontakte or other services using Basik Autentification?
Good afternoon!
The question is about working with api through spring boot java, it may seem silly, but still, the puzzle in my head cannot yet be built correctly.
There is the following description of obtaining an authorization token:
Bearer Token
Generates a bearer token from the client key and client secret using Basic Authorization using the api client key and api client secret.
Your client key and client secret must be presented using Basic Authorization where the header is:
Authorization: Basic {Base-64 encode of:[Your Api Client Key]:[Your Api Client Secret]}
//EXAMPLE: Authorization: Basic ZXdocVVHWWNM....IZjBKdjRrMUlHRFpNWVRMZG
All future requests must use Bearer Authorization where the header is:
Authorization: Bearer {Bearer Token}
//EXAMPLE: Authorization: Bearer eyxCbGciOiJIUzI1Nis...5xywLoYVqTcXDexbC8
/api/Authentication
{
"bearer_token": "eyxCbGciOiJIUzI1Nis...5xywLoYVqTcXDexbC8",
"expiration": "2018-05-01T15:20:22Z",
"ticks": 636607848220000000
}
/api/Rentals
[
{
"rentalId": "fe9cd4ce-47cb-40fb-850e-8f41d238ada0",
"timeRemaining": "13.21:59:02.0523196",
"expiration": "2019-07-22T23:47:15Z",
"maxRentalLifetime": "2019-09-01T12:45:00Z",
"recurringCost": 90,
"autoRenew": true,
"emailReminder": true,
"rentalTargets": [
{
"targetId": 3,
"name": "Airbnb",
"dailyFreeQuota": 0,
"number": "9876543210"
},
{
"targetId": 33,
"name": "Google",
"dailyFreeQuota": 5,
"number": "9876543210"
}
]
},
{
"rentalId": "98c6ba2e-ee43-4ce8-a92f-a088a615fcc3",
"timeRemaining": "6.23:37:56.1791903",
"expiration": "2019-07-16T05:55:56Z",
"maxRentalLifetime": "2019-08-22T20:27:00Z",
"recurringCost": 60,
"autoRenew": false,
"emailReminder": true,
"rentalTargets": [
{
"targetId": 72,
"name": "Tinder",
"dailyFreeQuota": 5,
"number": "8765432109"
}
]
}
]
Answer the question
In order to leave comments, you need to log in
Hello!
You can use RestTemplate to work with APIs of other sites.
The article describes how to work with it. How to send POST GET and other requests. How to receive responses and how to parse this response in pojo
https://www.baeldung.com/rest-template
Here, for example, is the implemented method for getting the status of the hotelbeds service
public HttpStatus getStatus() {
RestTemplate restTemplate = new RestTemplate();
HttpEntity httpEntity = new HttpEntity(hotelbedsAuth.hotelsAuth());
ResponseEntity<String> response = restTemplate.exchange(
hotelbedsProperties.getHotelBaseUrl()+"/status", HttpMethod.GET, httpEntity, String.class);
return response.getStatusCode();
}
Once the bearer token expires, you will have to use Basic Authorization again to obtain a new bearer token.
@Scheduled
to run the token refresh method using the cron at a given time interval Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question