Paul Fournel
Paul Fournel

Reputation: 11207

Auth with Java EE using the soundcloud API

I am building a JavaEE server side application using the sound cloud API (https://github.com/soundcloud/java-api-wrapper). I cannot make the auth work, I must be doing something wrong. Actually I can make a request just after beeing redirected by the URI, but when I try to auth the user with a token it fails.

I have 3 Servlet. They all call functions from the testSC class.

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;
   }

 }

I think the problem comes from the wrapper.setToken(to); because when I try with

HttpResponse resp = wrapper.get(Request.to("search/suggest?q=toto"));

I have a correct response. when I output the token in the console, here is what I get

Token{access='1-58925-4628866-9b917c45bdfba2f9', refresh='null', scope='null', expires=null}

Upvotes: 0

Views: 259

Answers (1)

Paul Fournel
Paul Fournel

Reputation: 11207

I know what went wrong. I used this answer(SoundCloud Official Java ApiWrapper, requests with saved token get rejected with 401) to store the token as two strings. But I am using non-expiring tokens.

When you output the token in the console you want:

Token{access='1-58925-4628866-7ec476b4b519dea0', refresh='null', scope='non-expiring', expires=null}

and not:

Token{access='1-58925-4628866-7ec476b4b519dea0', refresh='null', scope='null', expires=null}

So when you build the token, you must specify:

String access = (String) session.getAttribute("access");
String refresh = (String) session.getAttribute("refresh");
Token t = new Token(access, refresh, "non-expiring");

Upvotes: 1

Related Questions