0

Working with an OAuth API I need to retrieve the access_token attribute. Send the login information works fine and I do get a proper response back. The response is formatted as such:

access_token=SOMETOKEN&login=SOMELOGIN&apiKey=SOMEAPIKEY

Currently I'm getting the response as such:

org.apache.http.HttpResponse response = httpclient.execute(httppost);
String content = convertStreamToString(response.getEntity().getContent());

Where the second line gets the String of the content as described. So what's coming back is an URL Encoded string, and I need to get the access_token value of that. However I'm not sure how to do this, I can't seem to find a method or anything for doing this, Would I have to parse it or is there some other methods to do it?

2 Answers 2

0

Try URLDecoder.decode(content)

If that's the only problem, that should work.

Edit: I guess you already know this and you are looking for something more automatic, but just in case. I havent tested it.

String[] splitted = content.split("&");

//detect token
String token;
for(int i=0;i<splitted.length;i++){
if(splitted[i].startsWith("token"){
 String[] tokenSplitted = splitted[i].split("=");
token = tokenSplitted[1];
}
}
1
  • No I dont need to decode it, I need to get a specific argument of the String that could look like: access_token=n6hfcq3iio&login=username&apiKey=duksqmo98f So what I need to extract is n6hfcq3iio
    – Jacob
    Mar 7, 2012 at 17:20
0

I actually just found a solution to this located in the Twitter Api Me (http://kenai.com/projects/twitterapime/pages/Home) StringUtil class, I'm not gonna paste it here, but its open sorce so if anyone needs it, go get it from there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.