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
Developer
ushernut
Posts: 55
Registered: ‎09-09-2008
My Device: Not Specified

Re: OAuth / OAuth workaround?

I am wondering how you guys create Nonce, because twitter has mentioned, it needs to be ASCII, with this example code, it created just a normal string. 

 

any idea?

 

Developer
ushernut
Posts: 55
Registered: ‎09-09-2008
My Device: Not Specified

Re: OAuth / OAuth workaround?

Finally I made it....to answer my own question, yes, we can use this nonce() method that posted here.

 

I would suggest people follow http://dev.twitter.com/pages/auth ,step by step, when you test, change your parameters value same as this tutorial, and check if you can create same signature.

 

The example post on first page has lots of small error, this is why I suggest follow twitter tutorial.

Contributor
salmanfarisu
Posts: 17
Registered: ‎08-04-2010
My Device: Not Specified

Re: OAuth / OAuth workaround? need help ...

[ Edited ]

hi bherk2

 

i am a newbie to this blackberry world , and i got a task to integrate twitter in our blackberry app. 

i don't know how to post a request to get a ''Response''., and when we get a Response(200) what should i do with that ... these all are confusing me .

 

Can please give me some sample code .ie i saw you are  solved the problem what u faced .

 

this is my mail id : itskrishnaraj@gmail.com

 

Thanks

Kris 

 

SalmanFaris
Contributor
salmanfarisu
Posts: 17
Registered: ‎08-04-2010
My Device: Not Specified

Re: OAuth / OAuth workaround?

Hi 

 

Can u pls send me the methods getTwitterMessage() and  PreferencesStore . coz m a newbie to this network connection and all .. i m trying to use this for my blackberry app to integrate twitter.

 

thanks and regards 

kris

SalmanFaris
Contributor
salmanfarisu
Posts: 17
Registered: ‎08-04-2010
My Device: Not Specified

Re: OAuth / OAuth workaround?

Hi zany 

 

i m a newbie to this blackberry world . i got task to integrate twiiter to our blackberry app.

 

i have tried the above code and m getting errors like 401 , if ur succeeded  with that code ...can u pls send me that code...it will be very helpful for me

 

 

mail id : itskrishnaraj@gmail.com

 

Regards 

kris

 

 

SalmanFaris
Developer
trivedirujuta
Posts: 528
Registered: ‎10-06-2009
My Device: 9800,BB Dev Alpha,z10 limited edition

Re: OAuth / OAuth workaround?

Hi,

I have used your code and able to get oauth_token.But not able to proceed for authorize token.Can you please help how to open it in browser n get oauth_verifier?

Rujuta Trivedi
Contributor
salmanfarisu
Posts: 17
Registered: ‎08-04-2010
My Device: Not Specified

Re: OAuth / OAuth workaround?

Hi 

 

Somehow i managed to get the pin number in my custom browser (blackberry).now what should i do with that pin .??.i want to update my twitter status . how will i do this task using this pin number.??

 

Hey trivedirujuta , if want any code help i can do something ...

 

Thanks & regards

Kris

SalmanFaris
Developer
trivedirujuta
Posts: 528
Registered: ‎10-06-2009
My Device: 9800,BB Dev Alpha,z10 limited edition

Re: OAuth / OAuth workaround?

Hi Please can you help me with that code?Atleast I can try further with your code.Please provide it.

 

Thanks in advance

Rujuta Trivedi
Contributor
salmanfarisu
Posts: 17
Registered: ‎08-04-2010
My Device: Not Specified

integrating twitter with BB app

[ Edited ]

 

Hi 
Here i am posting my experience in integrating twitter with a blackberry application .
i have learned all the steps from twitter site used the code that i got from blackberry forum to get request_token and Access_token 
Here i am posting both methods (courtesy : klerisson ). made some changes to work it properly
 static long timestamp() 
{
return new Date().getTime()/1000;
}
private static String nonce() 
{
return Long.toString(Math.abs(RandomSource.getLong()));
}
public static String requestToken()
{
String url = Const.REQUEST_TOKEN_URL;
String header = oauth_header(url, HttpProtocolConstants.HTTP_METHOD_GET);
String requestTokenUrl = concatURL(url, header);
HttpConnection httpConn = null;
InputStream input = null;
try
{
httpConn = (HttpConnection) Connector.open(requestTokenUrl); // kris connection
httpConn.setRequestMethod(HttpProtocolConstants.HTTP_METHOD_GET);
httpConn.setRequestProperty("WWW-Authenticate","OAuth realm=http://twitter.com/");
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
input = httpConn.openDataInputStream();
int resp = httpConn.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());
message = httpConn.getResponseMessage();
}
return message;//(getTwitterMessage(httpConn.getResponseCode()));
}
catch (IOException e) 
{
return "exception";
}
catch (Exception nc) 
{
return "noConnection";
} finally {
try {
httpConn.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
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(Const.OAUTH_VERIFIER, PinPopup.texts.getText());
}
pairs.put(Const.OAUTH_VERSION, "1.0");
String sig = signature(method, url, pairs);
//pairs.put(Const.OAUTH_SIGNATURE, escape(sig));
StringBuffer header_sb = new StringBuffer();
if(method.equals("GET"))
{
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(Const.OAUTH_VERSION).append("=").append("1.0");
}
else if(method.equals("POST"))
{
System.out.println("posssssssssst");
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(Const.OAUTH_VERIFIER).append("=").append((PinPopup.texts.getText())).append("&");
}
header_sb.append(Const.OAUTH_VERSION).append("=").append("1.0");
}
return header_sb.toString();
private static String signature(String method, String requestURL, Hashtable pairs) 
{
StringBuffer sb = new StringBuffer();
String[] keys = new String[pairs.size()]; 
Enumeration e = pairs.keys();
int i = 0;
while(e.hasMoreElements()) 
{
String k = (String)e.nextElement();
keys[i++] = k + "=" + URLUTF8Encoder.encode((String)pairs.get(k));
}
Arrays.sort(keys, new Comparator() 
{
public int compare(Object arg0, Object arg1) 
{
return ((String)arg0).compareTo((String)arg1);
}
});
for(i = 0; i < keys.length; i++) 
{
sb.append(keys[i]).append('&');
}
sb.deleteCharAt(sb.length()-1);
String msg = method.toUpperCase() +"&" + URLUTF8Encoder.encode(requestURL) + "&" + URLUTF8Encoder.encode(sb.toString());
System.out.println(msg);
StringBuffer key = new StringBuffer();
if(Const.consumerSecret != null) key.append(URLUTF8Encoder.encode(Const.consumerSecret));
key.append('&');
if(Const.tokenSecret != null)
{
key.append(URLUTF8Encoder.encode(Const.tokenSecret));
}
try 
{
return hmacsha1(key.toString(), msg);
} catch (Exception ex) {
return null;
}
}
private static String hmacsha1(String key, String message)
throws CryptoTokenException, CryptoUnsupportedOperationException, IOException 
{
HMACKey k = new HMACKey(key.getBytes());
HMAC hmac = new HMAC(k, new SHA1Digest());
hmac.update(message.getBytes());
byte[] mac = hmac.getMAC();
return Base64OutputStream.encodeAsString(mac, 0, mac.length, false, false);
public static String concatURL(String url, String header){
String newurl=url;
header = header.replace(',', '&');
newurl = newurl+"?"+header;
return newurl;
}
public static String accessToken()
{
// update=true;
OutputStream os = null;
InputStream input = null;
HttpConnection httpConn = null;
String url = Const.ACCESS_TOKEN_URL;
String header = oauth_header(url, HttpProtocolConstants.HTTP_METHOD_POST);
try
{
httpConn = (HttpConnection) Connector.open(url); // kris connection
httpConn.setRequestMethod(HttpProtocolConstants.HTTP_METHOD_POST);
httpConn.setRequestProperty("WWW-Authenticate","OAuth realm=http://twitter.com/");
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConn.setRequestProperty("Content-Length", Integer.toString(header.getBytes().length));
os = httpConn.openOutputStream();
os.write(header.getBytes());
//os.flush();
os.close();
os = null;
input = httpConn.openDataInputStream();
int resp = httpConn.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();
// Dialog.alert(httpConn.getResponseMessage());
System.out.println("aceesssssssss token --------------"+httpConn.getResponseMessage());
token[0] = content.substring(content.indexOf((Const.OAUTH_TOKEN+"="))+(Const.OAUTH_TOKEN+"=").length(), content.indexOf('&'));
token[1] = content.substring(content.indexOf((Const.OAUTH_TOKEN_SECRET+"="))+(Const.OAUTH_TOKEN_SECRET+"=").length(), content.indexOf("&user"));
// token[1] = content.substring(content.indexOf((Const.OAUTH_TOKEN_SECRET+"="))+(Const.OAUTH_TOKEN_SECRET+"=").length(), content.indexOf("&user"));
// Const.token =token[0];
// Const.tokenSecret =token[1];
// PreferencesStore ps = new PreferencesStore(); //kris commeted 
// ps.setToken(token); //kris commeted 
message_1 = httpConn.getResponseMessage();
}
return message_1;//(getTwitterMessage(httpConn.getResponseCode()));
} catch (IOException e) {
return "exception";
} catch (Exception nc) {
return "noConnection";
} finally {
try {
httpConn.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} [/color]
i used above code for get request token and access the tokens to reach restricted area.
and to update twitter status i used the below code
[color=navy] public static void StatusUpdate() throws CryptoTokenException,CryptoUnsupportedOperationException, IOException 
{
String oauth_signature_method = "HMAC-SHA1";
String oauth_timestamp = String.valueOf(timestamp());
String oauth_nonce = nonce();
String oauth_version = "1.0";
System.out.println("curent time --"+curTime());
String postBody = "status="+ URLUTF8Encoder.encode("Join FanCharge ( "+curTime()+" )");
String baseString = "POST&" 
+ URLUTF8Encoder.encode("update_url") 
+ "&oauth_consumer_key%3D" + Const.consumerKey
+ "%26oauth_nonce%3D" + oauth_nonce
+ "%26oauth_signature_method%3D" + oauth_signature_method
+ "%26oauth_timestamp%3D" + oauth_timestamp
+ "%26oauth_token%3D" + token[0] 
+ "%26oauth_version%3D" + oauth_version + "%26"
+ URLUTF8Encoder.encode(postBody);
String signingSecret = URLUTF8Encoder.encode(Const.consumerSecret)+ "&" + URLUTF8Encoder.encode(token[1]);
String signature = hmacsha1(signingSecret, baseString);
System.out.println(signature);
String header = new StringBuffer("OAuth realm=\"").append(URLUTF8Encoder.encode(""))
.append("\", oauth_consumer_key=\"").append(URLUTF8Encoder.encode(Const.consumerKey))
.append("\", oauth_version=\"").append(oauth_version)
.append("\", oauth_token=\"").append(URLUTF8Encoder.encode(token[0]))
.append("\", oauth_timestamp=\"").append(URLUTF8Encoder.encode(oauth_timestamp)) 
.append("\", oauth_nonce=\"").append(URLUTF8Encoder.encode(oauth_nonce)) 
.append("\", oauth_signature_method=\"").append(URLUTF8Encoder.encode(oauth_signature_method))
.append("\", oauth_signature=\"").append(URLUTF8Encoder.encode(signature)).append("\"").toString();
UpdateMyNewStatus(header, postBody);
}
public static void UpdateMyNewStatus(String auth_header, String body)
{
String url = "update_url";
HttpConnection httpConn = null;
InputStream input = null;
OutputStream os = null;
try 
{
httpConn = (HttpConnection) Connector.open(url); // kris connection
httpConn.setRequestMethod(HttpProtocolConstants.HTTP_METHOD_POST);
httpConn.setRequestProperty("WWW-Authenticate","OAuth realm=http://twitter.com/");
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String authHeader = auth_header;
httpConn.setRequestProperty("Authorization", authHeader);
System.out.println(authHeader);
String postBody = body;
httpConn.setRequestProperty("Content-Length", Integer.toString(postBody.getBytes().length));
os = httpConn.openOutputStream();
os.write(postBody.getBytes());
os.close();
os = null;
input = httpConn.openDataInputStream();
int resp = httpConn.getResponseCode();
// Dialog.alert(httpConn.getResponseMessage());
// if (resp == HttpConnection.HTTP_OK) {
StringBuffer buffer = new StringBuffer();
int ch;
while ((ch = input.read()) != -1) {
buffer.append((char) ch);
}
String content = buffer.toString();
Dialog.alert(content);
System.out.println(content);
// }
} catch (Exception e) {
} finally {
try {
httpConn.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static Date curTime(){
Calendar cal = Calendar.getInstance();
return cal.getTime();
}  
and now its working like a charm .. 
if u want u can use these methods and u have any doubt fell free to ask me ...
mail id : krishnaraj@sipl.co.in
Cheers
Krishnaraj

 

SalmanFaris
Trusted Contributor
milanvishal
Posts: 201
Registered: ‎12-17-2010
My Device: Not Specified

Re: integrating twitter with BB app

Hi , which library you use for Authentication?

I looked at your code.

It seems good.

But I can't get that "OAUTH_TOKEN" & "OAUTH_TOKEN_SECRET"?

So from where I cat get it ?

Thank you.