Home > Enterprise >  How to get data from rest API?
How to get data from rest API?

Time:01-01

I want to get data from this social media: https://vk.com/dev/authcode_flow_user I wrote some code and I got error: (401 Unauthorized: "{"error":"invalid_client","error_description":"client_id is incorrect"}"). It happened in first step of guide. But I swear I wrote client_id parameter properly and it works when I try to send request in Postman and in browser. Please, can someone tell me what's wrong?

This is my code in Spring app:

@SpringBootApplication
public static void main(String[] args) {
SpringApplication.run(BlogApplication.class, args);
Map<String, String> uriVariables = new HashMap<String, String>();
    uriVariables.put("client_id", "<here my id, I dont want to show it>");
    uriVariables.put("display", "page");
    uriVariables.put("redirect_uri", "http://vk.com");
    uriVariables.put("scope", "friends");
    uriVariables.put("response_type", "code");
    uriVariables.put("v", "5.131");

final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

final HttpEntity<String> entity = new HttpEntity<String>(headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Map> response = restTemplate.exchange("https://oauth.vk.com/authorize?", HttpMethod.GET, entity, Map.class, uriVariables);
System.out.println(response.getBody());

CodePudding user response:

The reason why it does not work, is because this is not a REST endpoint where you expect to get a single response.

This call that you make, authorizes users and after that, makes a redirect into another URI that you provide in the first URL.

If a user is not authorized in VK in current browser they will be offered to enter a login and a password in the dialog window.

Probably in the browser you have already authorized yourself once with the auth window and after that it kept working.

  • Related