N
N
nolouds2021-12-30 22:04:00
Spring
nolouds, 2021-12-30 22:04:00

How to fix "client_id incorrect" error and get data from vk api?

You need to get data from this URL. I created a VK application, entered the ID of my application instead of client_id, and everything worked manually in the browser:

https://oauth.vk.com/authorize?client_id=1&display=page&redirect_uri=http://example.com/callback&scope=friends&response_type=token&v=5.131


Then I tried to make a get request in Postman and got a 401Unauthorized error.
And it's the same in Spring itself: 401 Unauthorized: "{"error":"invalid_client","error_description":"client_id is incorrect"}".
I am sure that the id entered is correct, double-checked. Here is the code:
@SpringBootApplication
public class BlogApplication {

  private static final Logger log =
  LoggerFactory.getLogger(BlogApplication.class);

  public static void main(String[] args) {
    SpringApplication.run(BlogApplication.class, args);
    RestTemplate restTemplate = new RestTemplate();
    Map<String, String> uriVariables = new HashMap<String, String>();
    uriVariables.put("client_id", "Здесь мой id, палить не буду, но он написан правильно 100%");
    uriVariables.put("display", "page");
    uriVariables.put("redirect_uri", "http://vk.com");
    uriVariables.put("scope", "friends");
    uriVariables.put("response_type", "token");
    uriVariables.put("v", "5.131");
    Access access = restTemplate.getForObject("https://oauth.vk.com/authorize", Access.class, uriVariables);
    log.info(access.toString());
  }

}


Access.java (And this is just a model of the object that I want to get), created three fields, because after the request I have to get three data units in the response, they are written as "access_token", "expires_in", "user_id".
@JsonIgnoreProperties
public class Access {
    private Long token;
    private String expiresIn;
    private String userId;

    public Access() {}

    public Long getToken() {
        return token;
    }

    public void setToken(Long token) {
        this.token = token;
    }

    public String getExpiresIn() {
        return expiresIn;
    }

    public void setExpiresIn(String expiresIn) {
        this.expiresIn = expiresIn;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    @Override
    public String toString() {
        return "Access [expiresIn=" + expiresIn + ", token=" + token + ", userId=" + userId + "]";
    }
    
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question