我正在使用声音云API(https://github.com/soundcloud/java-api-wrapper)构建JavaEE服务器端应用程序。我不能让auth工作,我一定做错了。实际上,我可以在被URI重定向后立即发出请求,但是当我尝试使用令牌授权用户时,它会失败。
我有3个Servlet。它们都从testSC类调用函数。
public class testSC {
public ApiWrapper wrapper;
testSC(){
System.out.println(URI.create("http://localhost:8080/Test/loginp"));
wrapper = new ApiWrapper("*****", "******", URI.create("http://localhost:8080/Test/loginp"), null);
}
public String loginStage1(){
URI uri = wrapper.authorizationCodeUrl(Endpoints.CONNECT, Token.SCOPE_NON_EXPIRING);
return uri.toString();
}
public Token loginStage2(String code) throws IOException{
return wrapper.authorizationCode(code);
}
public String getSC(Token to) {
wrapper.setToken(to);
try {
HttpResponse resp = wrapper.get(Request.to("me"));
HttpEntity entity = resp.getEntity();
String content = EntityUtils.toString(entity);
System.out.println(content);
out = content;
} catch (IOException e) {
e.printStackTrace();
}
return out;
}
}
我认为问题来自wrapper.setToken(to);
,因为当我尝试使用
HttpResponse resp = wrapper.get(Request.to("search/suggest?q=toto"));
我有正确的回答。当我在控制台中输出令牌时,这就是我得到的
Token{access='1-58925-4628866-9b917c45bdfba2f9', refresh='null', scope='null', expires=null}
答案 0 :(得分:1)
我知道出了什么问题。我使用这个答案(SoundCloud Official Java ApiWrapper, requests with saved token get rejected with 401)将令牌存储为两个字符串。但我使用的是非过期令牌。
在控制台中输出所需的令牌时:
Token{access='1-58925-4628866-7ec476b4b519dea0', refresh='null', scope='non-expiring', expires=null}
而不是:
Token{access='1-58925-4628866-7ec476b4b519dea0', refresh='null', scope='null', expires=null}
因此,在构建令牌时,必须指定:
String access = (String) session.getAttribute("access");
String refresh = (String) session.getAttribute("refresh");
Token t = new Token(access, refresh, "non-expiring");