A
A
Albert2020-10-04 11:06:16
Java
Albert, 2020-10-04 11:06:16

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

See https://tools.ietf.org/html/rfc7617 for more details on Basic Authorization. This token WILL EXPIRE, after which a new bearer token will need to be obtained. This token must be presented in future requests in the form of a Authorization: Bearer header.
All future requests must use Bearer Authorization where the header is:
        
Authorization: Bearer {Bearer Token}
//EXAMPLE:    Authorization: Bearer eyxCbGciOiJIUzI1Nis...5xywLoYVqTcXDexbC8

Once the bearer token expires, you will have to use Basic Authorization again to obtain a new bearer token. See https://tools.ietf.org/html/rfc6750 for more details on Bearer Tokens.

Server URL:
/api/Authentication
Reply:
{

    "bearer_token": "eyxCbGciOiJIUzI1Nis...5xywLoYVqTcXDexbC8",
    "expiration": "2018-05-01T15:20:22Z",
    "ticks": 636607848220000000

}

Well, for example, the implementation of a GET request to api, and in the request you need to take into account the received token during authorization.

Server URL:
/api/Rentals
Reply:
[
  {
    "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"
      }
    ]
  }
]


How can I organize work with Spring boot with api, I remember collecting requests in php via curl and sending them to the server I received answers, I sorted it out and smiled, there’s something I can’t understand if I’m a fool, or skis don’t go ..

If anyone has an example similar work or what to read more specifically, as far as I understand, you need to work with rest full, but so far I can’t catch on anywhere to dig further.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2020-10-04
@6ETuK

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();
}

In general, read about RestTemplate.
Once the bearer token expires, you will have to use Basic Authorization again to obtain a new bearer token.

Here you can use CRON or annotation @Scheduledto run the token refresh method using the cron at a given time interval
https://www.baeldung.com/spring-scheduled-tasks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question