UniformInterfaceException in Java, Response status of 301


#1

Dear all, I have written the following section of code after following this tutorial:

https://netbeans.org/kb/docs/websvc/twitter-swing.html

// more code above

private static final String OAUTH_BASE_URL = “http://twitter.com/oauth”;
/**
* Please, specify the consumer_key string obtained from service API pages
/
private static final String CONSUMER_KEY = “zE7iBvNSdLeHa032aMZqtA”;
/
*
* Please, specify the consumer_secret string obtained from service API pages
*/
private static final String CONSUMER_SECRET = “my secret key here”;
private OAuthParameters oauth_params;
private OAuthSecrets oauth_secrets;
private OAuthClientFilter oauth_filter;
private String oauth_access_token;
private String oauth_access_token_secret;

    public void setResourcePath(String format) {
        String resourcePath = java.text.MessageFormat.format("statuses/user_timeline.{0}", new Object[]{format});
        webResource = client.resource(BASE_URI).path(resourcePath);
    }

    /**
     * @param responseType Class representing the response
     * @param since query parameter
     * @param since_id query parameter
     * @param page query parameter
     * @param count query parameter
     * @return response object (instance of responseType class)
     */
    public <T> T getUserTimeline(Class<T> responseType, String since, String since_id, String page, String count) throws UniformInterfaceException {
        String[] queryParamNames = new String[]{"since", "since_id", "page", "count"};
        String[] queryParamValues = new String[]{since, since_id, page, count};
        return webResource.queryParams(getQueryOrFormParams(queryParamNames, queryParamValues)).accept(javax.ws.rs.core.MediaType.TEXT_XML).get(responseType);
    }

    private MultivaluedMap getQueryOrFormParams(String[] paramNames, String[] paramValues) {
        MultivaluedMap<String, String> qParams = new com.sun.jersey.api.representation.Form();
        for (int i = 0; i < paramNames.length; i++) {
            if (paramValues[i] != null) {
                qParams.add(paramNames[i], paramValues[i]);
            }
        }
        return qParams;
    }

    public void close() {
        client.destroy();
    }

    /**
     * You need to call this method at the beginning to authorize the application to work with user data.
     * The method obtains the OAuth access token string, that is appended to each API request later.
     */
    public void login() throws IOException, UniformInterfaceException {
        Form requestTokenResponse = getOAuthRequestToken();
        String oauth_verifier = authorizeConsumer(requestTokenResponse);
        Form accessTokenResponse = getOAuthAccessToken(requestTokenResponse, oauth_verifier);
        oauth_access_token_secret = accessTokenResponse.getFirst("my secret token here");
        oauth_access_token = accessTokenResponse.getFirst("561089922-KrK6UmFMxQkv1QgCiYrWSVgITCFdZi03XXX2K1Of");
    }

    private Form getOAuthRequestToken() throws UniformInterfaceException {
        WebResource resource = client.resource(OAUTH_BASE_URL).path("/request_token");
        oauth_params = new OAuthParameters().consumerKey(CONSUMER_KEY).signatureMethod(com.sun.jersey.oauth.signature.HMAC_SHA1.NAME).version("1.0").nonce().timestamp();
        oauth_secrets = new OAuthSecrets().consumerSecret(CONSUMER_SECRET);
        oauth_filter = new OAuthClientFilter(client.getProviders(), oauth_params, oauth_secrets);
        resource.addFilter(oauth_filter);
        return resource.get(Form.class);
    }


I am relatively new to Java, and am getting this stacktrace when I try and run the program:

Exception in thread "AWT-EventQueue-0" com.sun.jersey.api.client.UniformInterfaceException: GET http://twitter.com/oauth/request_token returned a response status of 301 Moved Permanently
	at com.sun.jersey.api.client.WebResource.handle(WebResource.java:686)
	at com.sun.jersey.api.client.WebResource.get(WebResource.java:191)
	at twitterclient.TwitterJFrame$Twitter_OAuth_user_timeline__format_JerseyClient.getOAuthRequestToken(TwitterJFrame.java:269)
	at twitterclient.TwitterJFrame$Twitter_OAuth_user_timeline__format_JerseyClient.login(TwitterJFrame.java:256)
	at twitterclient.TwitterJFrame.initUserInfo(TwitterJFrame.java:130)
	at twitterclient.TwitterJFrame.<init>(TwitterJFrame.java:35)
	at twitterclient.TwitterJFrame$2.run(TwitterJFrame.java:174)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
	at java.awt.EventQueue.access$200(EventQueue.java:103)
	at java.awt.EventQueue$3.run(EventQueue.java:688)
	at java.awt.EventQueue$3.run(EventQueue.java:686)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

I would be grateful if someone could explain to me how to solve this.

Many thanks,

Michael Nares

#2

You must use https for your oauth steps. We’re returning a 301 to say that the http:// version of that page is actually over at https://.

Update line #3 to

private static final String OAUTH_BASE_URL = "https://twitter.com/oauth";

#3

Thanks for that, however I’m now getting this:

Type oauth_verifier string (taken from callback page url):8337347 Exception in thread "AWT-EventQueue-0" com.sun.jersey.api.client.UniformInterfaceException: GET https://twitter.com/oauth/access_token returned a response status of 401 Unauthorized at com.sun.jersey.api.client.WebResource.handle(WebResource.java:686) at com.sun.jersey.api.client.WebResource.get(WebResource.java:191) at twitterclient.TwitterJFrame$Twitter_OAuth_user_timeline__format_JerseyClient.getOAuthAccessToken(TwitterJFrame.java:277) at twitterclient.TwitterJFrame$Twitter_OAuth_user_timeline__format_JerseyClient.login(TwitterJFrame.java:258) at twitterclient.TwitterJFrame.initUserInfo(TwitterJFrame.java:130) at twitterclient.TwitterJFrame.(TwitterJFrame.java:35) at twitterclient.TwitterJFrame$2.run(TwitterJFrame.java:174) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:688) at java.awt.EventQueue$3.run(EventQueue.java:686) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:697) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

How do I deal with this 401 error please?