I want to use the VK API to send messages on behalf of the group from the web application (I use WildFly). The SendMessage method is called from the servlet, here is the code:
public class VkMessageSender implements MessageSender{ private static final Integer APP_ID = 123; // id приложения private static final String CLIENT_SECRET = "..."; // защищенный ключ private static final String REDIRECT_URI = "https://oauth.vk.com/blank.html"; private static final String OAUTH_URI = "https://oauth.vk.com/authorize?client_id=" + APP_ID + "&display=page&client_secret=" + CLIENT_SECRET + "&redirect_uri=" + REDIRECT_URI + "&scope=messages&response_type=code&v=5.68"; private static final Integer GROUP_ID = 123; // id группы, из которой отправляются сообщения HttpServletResponse response; // получаем из сервлета TransportClient transportClient = HttpTransportClient.getInstance(); VkApiClient vk = new VkApiClient(transportClient); public void SendMessage(Integer user, String message) throws ServletException, IOException { response.sendRedirect(OAUTH_URI); String code = response.getHeader("code"); try { GroupAuthResponse authResponse = vk.oauth() .groupAuthorizationCodeFlow(APP_ID, CLIENT_SECRET, REDIRECT_URI, code) .execute(); GroupActor actor = new GroupActor(GROUP_ID, authResponse.getAccessTokens().get(GROUP_ID)); vk.messages().send(actor).message(message).userId(user); } catch (Throwable e){ throw new ServletException(e.getMessage()); } } public VkMessageSender(HttpServletResponse response){ this.response = response; } } The code is successfully compiled, after clicking on the button, the browser is redirected to oauth.vk.com, in the address bar I see the code I need. But at the same time no message is sent, but in the logs I see:
2017-10-29 04:48:44,442 ERROR [io.undertow.request] (default task-48) UT005023: Exception handling request to /sendmsg/blank.html: javax.servlet.ServletException: Internal API server error. Wrong status code: 401. Content: {"error":"invalid_grant","error_description":"Code is invalid or expired."} at servletpackage.VkMessageSender.SendMessage(VkMessageSender.java:49) at servletpackage.MainServlet.doPost(MainServlet.java:20) at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62) at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131) at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46) at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64) at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60) at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77) at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50) at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:284) at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:263) at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81) at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:174) at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202) at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:793) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) What am I doing wrong? How can I correctly extract the code from the parameters of the GET method, with which the VC responds to my redirect?