Search code examples
javaflickr

Java: Flickr OAuth Signature Invalid


so I built an app which lets user login and link their Flickr account to my app, it logins fine and stores AccessToken in my database for user to avoid linking Flickr every time they login. I have this code:

 OAuthService service = new ServiceBuilder()
                .provider(FlickrApi.class)
                .apiKey("apiKey")
                .apiSecret("apiSecret")
                .build();
        Token flickReq = service.getRequestToken();
        String authUrl = service.getAuthorizationUrl(flickReq);
        try{
        MSConnection msc = new MSConnection();
        Token accessToken = msc.getToken(userId_acc); // This gets AccessToken from database.
        OAuthRequest req = new OAuthRequest(Verb.POST, "https://api.flickr.com/services/rest/");
        req.addQuerystringParameter("method", "flickr.test.login");
        service.signRequest(accessToken, req);
        Response resp = req.send();
        System.out.println(resp.getBody());

in response I get: oauth_problem=signature_invalid

What am I doing wrong?


Solution

  • Solved problem, as it turns out: When you are generating Token using AccessToken you stored and Secret - Secret isn't the API Secret, it's accesstoken secret which you get by:

    Token accessToken = service.getAccessToken(flickReq, v); // Original accessToken you get from Flickr and users entered code.
    
    String tokenSecret = accessToken.getSecret();
    

    So you store these two variables for user, then whenever you want to validate users Flickr Details without need to authorize again and enter code - you just use: accessToken + tokenSecret

    Token tok = new Token(getToken(userId_acc), getSecret(userId_acc));
    

    Then you just sign request and send it off.

    OAuthRequest req = new OAuthRequest(Verb.POST, "https://api.flickr.com/services/rest/");
    req.addQuerystringParameter("method", "flickr.test.login");
    service.signRequest(tok, req);
    Response resp = req.send();
    

    That's it, hopefully this saves some time for somebody who comes across this problem, apparently Flickr API documentation doesn't really explain Java side of it well.