当我尝试运行时出现错误:
Caused by:
com.vk.api.sdk.exceptions.ClientException:
Internal API server error. Wrong status code: 401.
Content:
{"error":"invalid_grant","error_description":"Code is invalid or expired."}
起初我认为代码已经失去了有效性。
我又问了一遍,问题依旧。
像这样请求:
请告诉我,我哪里做错了?
package reversbot.services;
import com.vk.api.sdk.client.TransportClient;
import com.vk.api.sdk.client.VkApiClient;
import com.vk.api.sdk.client.actors.UserActor;
import com.vk.api.sdk.exceptions.ApiException;
import com.vk.api.sdk.exceptions.ClientException;
import com.vk.api.sdk.httpclient.HttpTransportClient;
import com.vk.api.sdk.objects.UserAuthResponse;
import com.vk.api.sdk.objects.wall.GetFilter;
import com.vk.api.sdk.objects.wall.responses.GetResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
@Service
public class VkBot {
@Value("${APP_ID}")
int APP_ID;
@Value("${CLIENT_SECRET}")
String CLIENT_SECRET;
@Value("${REDIRECT_URI}")
String REDIRECT_URI;
@Value("${code}")
String code;
@Scheduled(fixedDelay = 5000)
@PostConstruct
public void vkb() throws ClientException, ApiException {
TransportClient transportClient = new HttpTransportClient();
VkApiClient vk = new VkApiClient(transportClient);
UserAuthResponse authResponse = vk.oAuth()
.userAuthorizationCodeFlow(APP_ID, CLIENT_SECRET, REDIRECT_URI, code)
.execute();
UserActor actor = new UserActor(authResponse.getUserId(), authResponse.getAccessToken());
GetResponse getResponse = vk.wall().get(actor)
.ownerId(1)
.count(100)
.offset(5)
.filter(GetFilter.valueOf("all"))
.execute();
}
}
问题以不同的方式解决了。授权“获取用户访问密钥的隐式流程”解决了我的问题。