01-31-2012 10:12 AM
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().getConne ction(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_TOK EN+"="))+(Const.OAUTH_TOKEN+"=").length(), content.indexOf('&'));
Const.tokenSecret = content.substring(content.indexOf((Const.OAUTH_TOK EN_SECRET+"="))+(Const.OAUTH_TOKEN_SECRET+"=").len gth(), 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.TRAN SPORT_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.consumerKe y)).append(comma);
header_sb.append(Const.OAUTH_NONCE).append("=").ap pend(nonce).append(comma);
header_sb.append(Const.OAUTH_SIGNATURE).append("=" ).append(URLUTF8Encoder.encode(sig)).append(comma) ;
header_sb.append(Const.OAUTH_SIGNATURE_METHOD).app end("=").append(URLUTF8Encoder.encode(Const.SIGNAT URE_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("=").ap pend(URLUTF8Encoder.encode(Const.token)).append(co mma);
}
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.consumerKe y)).append("&");
header_sb.append(Const.OAUTH_NONCE).append("=").ap pend(nonce).append("&");
header_sb.append(Const.OAUTH_SIGNATURE).append("=" ).append(URLUTF8Encoder.encode(sig)).append("&");
header_sb.append(Const.OAUTH_SIGNATURE_METHOD).app end("=").append(URLUTF8Encoder.encode(Const.SIGNAT URE_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("=").ap pend(URLUTF8Encoder.encode(Const.token)).append("& ");
//header_sb.append(OAUTH_VERIFIER).append("=").app end(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();
}
Solved! Go to Solution.
02-01-2012 09:18 AM
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