Search code examples
javaoauthscribe

Using Scribe, OAuth Echo doesn't seem to work with Twitpic


I'm trying to upload photos using Scribe, and it doesn't seem to work with getting an error:

Authentication challenged received is null

And I have no idea, due to the lack of documentation on the matter how to fix this.

The relevant code is:

try{
        OAuthRequest r = new OAuthRequest(Verb.POST, url);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        toSend.writeTo(out);
        r.addPayload(out.toByteArray());
        r.addHeader(toSend.getContentType().getName(), toSend.getContentType().getValue());

        OAuth10aServiceImpl oauth = (OAuth10aServiceImpl)oauthImpl;
        OAuthRequest sr = new OAuthRequest(Verb.GET, serviceProvider);
        sr.addOAuthParameter(OAuthConstants.TOKEN, oauthToken.getToken());
        sr.addOAuthParameter(OAuthConstants.REALM,"http://api.twitter.com/");
        oauth.addOAuthParams(sr, client._oauthToken);

        r.addHeader("X-Auth-Service-Provider",serviceProvider);
        String oauthHeader = oauth.api.getHeaderExtractor().extract(sr);
        r.addHeader("X-Verify-Credentials-Authorization", oauthHeader);

        System.out.println(r.getHeaders().get("X-Verify-Credentials-Authorization"));

        return r.send();
    }catch(Exception e){
        e.printStackTrace();
        return null;
    }

I'm thinking it's something with the consumer key, as it doesn't seem to be added to the signature, but I'm not too sure what's really going on :/

Any help would be appreciated


Solution

  • Fixed. Here is the code:

            OAuthRequest r = new OAuthRequest(Verb.POST, url);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            toSend.writeTo(out);
            r.addPayload(out.toByteArray());
            r.addHeader(toSend.getContentType().getName(), toSend.getContentType().getValue());
    
            OAuth10aServiceImpl oauth = (OAuth10aServiceImpl)client._oauth;
            OAuthRequest sr = new OAuthRequest(Verb.GET, serviceProvider);
            oauth.signRequest(client._oauthToken, sr);
    
            r.addHeader("X-Auth-Service-Provider",serviceProvider);
            String oauthHeader = sr.getHeaders().get("Authorization");
            r.addHeader("X-Verify-Credentials-Authorization", oauthHeader);
    

    For anyone wanting to know how this works, toSend is a HttpEntity containing the file and other StringBody parts and client._oauthToken is a Token object. r is my request to twitpic and serviceProvider is the Twitter API verify credentials url (which changes on some services like yfrog)