簡體   English   中英

Yahoo!是什么樣的數據? Messenger API返回請求嗎?

[英]What kind of data does Yahoo! Messenger API return for requests?

我看着雅虎! Messenger API文檔,以了解如何獲取訪問令牌,並發現了這一點:

呼叫看起來像這樣:

https://login.yahoo.com/WSLogin/V1/get_auth_token?&login=username&passwd=mypassword&oauth_consumer_key=consumerkey

調用的結果是單個值RequestToken

RequestToken=jUO3Qolu3AYGU1KtB9vUbxlnzfIiFRLP...

然后,此令牌用於第二個請求,該請求將PART交換為OAuth訪問令牌。 您可以在此處找到有關獲取訪問令牌的標准方法的更多信息。

我猜結果是標准結果,但是我不知道這是什么樣的數據。 我的意思是它不是XML或JSON。

我想將這樣的字符串轉換為JSON:

{
    RequestToken: "jU0..."
}

是否有任何標准轉換器/解析器,或者我必須構建一個?


另外,另一個請求可能如下所示:

Error=MissingParameters
ErrorDescription=Sorry, try again with all the required parameters.

我想將其轉換為JSON:

{
   Error: "MissingParameters",
   ErrorDescription: "Sorry, try again with all the required parameters."
}

構建這樣的解析器非常容易,但是我不想重新發明輪子。

我決定編寫自己的函數。 如果存在解析此類字符串的標准算法,請留下評論。

/*
 *  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;
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM