Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 10
Registered: ‎10-18-2011
My Device: Torch 9860
My Carrier: Movistar
Accepted Solution

Twitter 401 error on device

Hi i don´t know why my code crash on 9700 OS 5, i try in 9300 OS6, 9780 OS 6, and all devices with 0S 7 and the same code work as expected.

 

I take de code from this forum, i don´t remenber where, and change some lines and constants.

 

all devices return 200 code but 9700 give me a 401

 

please help

 

public static String requestToken(){
        String url = Const.REQUEST_TOKEN_URL;
        String header = oauth_header(url, HttpProtocolConstants.HTTP_METHOD_GET);
        String requestTokenUrl = concatURL(url, header);
        System.out.println(requestTokenUrl);
        HttpsConnection connection = null;
        InputStream input = null;
        try{
        	connection = (HttpsConnection) HttpUtils.getConfiguredConectionFactory().getConnection(requestTokenUrl).getConnection();
        	connection.setRequestMethod(HttpProtocolConstants.HTTP_METHOD_GET);
        	connection.setRequestProperty("WWW-Authenticate","OAuth realm=http://twitter.com/");
        	connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            input = connection.openDataInputStream();
            int resp = connection.getResponseCode();
            if (resp == HttpConnection.HTTP_OK) {
                StringBuffer buffer = new StringBuffer();
                int ch;
                while ( (ch = input.read()) != -1){
                    buffer.append( (char) ch);
                }
                String content = buffer.toString();
                Const.token = content.substring(content.indexOf((Const.OAUTH_TOKEN+"="))+(Const.OAUTH_TOKEN+"=").length(), content.indexOf('&'));
                Const.tokenSecret = content.substring(content.indexOf((Const.OAUTH_TOKEN_SECRET+"="))+(Const.OAUTH_TOKEN_SECRET+"=").length(), content.length());

            }else{
            	  StringBuffer buffer = new StringBuffer();
                  int ch;
                  while ( (ch = input.read()) != -1){
                      buffer.append( (char) ch);
                  }
                  String content = buffer.toString();
                  System.out.println("request Error: " + content);
            }
            return Const.token;
            //return (getTwitterMessage(connection.getResponseCode()));
        } catch (IOException e) {
            return "IOexception";
        } catch (TransactionException nc) {
            return "noConnection";
        } catch (Exception e) {
			return "Exeption";		
        } finally {
            try {
            	connection.close();
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 

public static ConnectionFactory getConfiguredConectionFactory(int timeout){
		int[] allow ={	
				TransportInfo.TRANSPORT_TCP_CELLULAR
		};
		int[] denny ={
				TransportInfo.TRANSPORT_MDS,
				TransportInfo.TRANSPORT_WAP,
				TransportInfo.TRANSPORT_TCP_WIFI,
				TransportInfo.TRANSPORT_WAP2,
				TransportInfo.TRANSPORT_BIS_B
				
		};
		ConnectionFactory factory = new ConnectionFactory();	
		factory.setEndToEndDesired(false);
		factory.setEndToEndRequired(false);
		factory.setPreferredTransportTypes( allow);		
		factory.setDisallowedTransportTypes(denny);		
		factory.setAttemptsLimit(3);
		factory.setConnectionMode(ConnectionFactory.ACCESS_READ_WRITE);
		factory.setTimeoutSupported(true);		
	
		factory.setTransportTypeOptions(TransportInfo.TRANSPORT_TCP_CELLULAR,telefonicaAPN );
		factory.setConnectionTimeout(timeout);
		factory.setTimeLimit(timeout);
                return factory;
}

 

public static String oauth_header(String url, String method) {
        String nonce = nonce();
        long timestamp = timestamp();
        Hashtable pairs = new Hashtable();

        pairs.put(Const.OAUTH_CONSUMER_KEY, Const.consumerKey);
        pairs.put(Const.OAUTH_NONCE, nonce);
        pairs.put(Const.OAUTH_SIGNATURE_METHOD, Const.SIGNATURE_METHOD);
        pairs.put(Const.OAUTH_TIMESTAMP, Long.toString(timestamp));
        if(Const.token != null) {
            pairs.put(Const.OAUTH_TOKEN, Const.token);
            //pairs.put(OAUTH_VERIFIER, verifier);
        }
        pairs.put(Const.OAUTH_VERSION, "1.0");
        pairs.put(Const.OAUTH_CALLBACK, Const.CALLBACK_URL);
        String sig = signature(method, url, pairs);
        //pairs.put(Const.OAUTH_SIGNATURE, escape(sig));

        StringBuffer header_sb = new StringBuffer();
        if(method.equals("GET"))
        {
        	String comma = ",";        	
            header_sb.append(Const.OAUTH_CONSUMER_KEY).append("=").append(URLUTF8Encoder.encode(Const.consumerKey)).append(comma);
            header_sb.append(Const.OAUTH_NONCE).append("=").append(nonce).append(comma);
            header_sb.append(Const.OAUTH_SIGNATURE).append("=").append(URLUTF8Encoder.encode(sig)).append(comma);
            header_sb.append(Const.OAUTH_SIGNATURE_METHOD).append("=").append(URLUTF8Encoder.encode(Const.SIGNATURE_METHOD)).append(comma);
            header_sb.append(Const.OAUTH_TIMESTAMP).append("=").append(Long.toString(timestamp)).append(comma);
            if(Const.token != null) {
                header_sb.append(Const.OAUTH_TOKEN).append("=").append(URLUTF8Encoder.encode(Const.token)).append(comma);
            }
            header_sb.append(Const.OAUTH_VERSION).append("=").append("1.0").append(comma);;
            header_sb.append(Const.OAUTH_CALLBACK).append("=").append(URLUTF8Encoder.encode(Const.CALLBACK_URL));
        }
        else if(method.equals("POST"))
        {        	
            header_sb.append(Const.OAUTH_CONSUMER_KEY).append("=").append(URLUTF8Encoder.encode(Const.consumerKey)).append("&");
            header_sb.append(Const.OAUTH_NONCE).append("=").append(nonce).append("&");
            header_sb.append(Const.OAUTH_SIGNATURE).append("=").append(URLUTF8Encoder.encode(sig)).append("&");
            header_sb.append(Const.OAUTH_SIGNATURE_METHOD).append("=").append(URLUTF8Encoder.encode(Const.SIGNATURE_METHOD)).append("&");
            header_sb.append(Const.OAUTH_TIMESTAMP).append("=").append(Long.toString(timestamp)).append("&");
            if(Const.token != null) {
                header_sb.append(Const.OAUTH_TOKEN).append("=").append(URLUTF8Encoder.encode(Const.token)).append("&");
                //header_sb.append(OAUTH_VERIFIER).append("=").append(escape(verifier)).append("&");
            }
            header_sb.append(Const.OAUTH_VERSION).append("=").append("1.0").append("&");
            header_sb.append(Const.OAUTH_CALLBACK).append("=").append(Const.CALLBACK_URL);
        }
        return header_sb.toString();
    } 

 

Highlighted
Contributor
Posts: 10
Registered: ‎10-18-2011
My Device: Torch 9860
My Carrier: Movistar

Re: Twitter 401 error on device

Well i found the solution of al my headache, the phone are the time incorrect and that the reason of the same code fail in this terminal