I looked in the Yahoo! Messenger API documentation to see how can I get the access token and I found this:
The call looks like this:
https://login.yahoo.com/WSLogin/V1/get_auth_token?&login=username&passwd=mypassword&oauth_consumer_key=consumerkey
The result of this call is a single value, the
RequestToken
:
RequestToken=jUO3Qolu3AYGU1KtB9vUbxlnzfIiFRLP...
This token is then used for a second request, which exchanges the PART for an OAuth access token. You can find out more information on the standard method getting an access token here.
I guess that the result is a standard result, but I don't know what kind of data is this. I mean that it isn't XML or JSON.
I would like to convert such a string to JSON:
{
RequestToken: "jU0..."
}
Is there any standard converter/parser or must I build one?
Also, another request can look like below:
Error=MissingParameters
ErrorDescription=Sorry, try again with all the required parameters.
and I want to convert it into JSON:
{
Error: "MissingParameters",
ErrorDescription: "Sorry, try again with all the required parameters."
}
It would be very easy to build such a parser, but I don't want to reinvent the wheel.
I decided to write my own function. If there is a standard algorithm to parse such a string leave a comment.
/*
* Transform a string like this:
*
* "Field1=123
* Field2=1234
* Field3=5"
*
* into an object like this:
*
* {
* "Field1": "123",
* "Field2": "1234",
* "Field3": "5",
* }
*
* */
function parseResponse (str) {
// validate the provided value
if (typeof str !== "string") {
throw new Error("Please provide a string as argument: " +
JSON.stringify(str));
}
// split it into lines
var lines = str.trim().split("\n");
// create the object that will be returned
var parsedObject = {};
// for every line
for (var i = 0; i < lines.length; ++i) {
// split the line
var splits = lines[i].split("=")
// and get the field
, field = splits[0]
// and the value
, value = splits[1];
// finally set them in the parsed object
parsedObject[field] = value;
}
// return the parsed object
return parsedObject;
};