XmlListModel - Host requires authentication



  • Hi,
    I'm trying to get data from Yammer with their API, with XmlListModel object without success. :-(
    I'm getting the following error: "Host requires authentication"

    If I try to access the URL (XmlListModel.source) via my web browser it works fine.

    Someone know how to solve this issue?

    thanks,
    Ayden.



  • Can you please post a code snippet of your code?



  • Yes, Although I'll have to register again to get new oauth_consumer_key :-)

    This code is inspired by the example: "http://wiki.forum.nokia.com/index.php/QML_OAuth":http://wiki.forum.nokia.com/index.php/QML_OAuth

    Because the code is too long, I'll split the post

    oauth.js
    @
    var oauth_token
    var oauth_token_secret

    function urlChanged(url) {
    console.debug("Start: urlChanged()");
    var authorized = false;
    var mUrl = url.toString();
    var code = "";
    if (mUrl.indexOf("https://www.yammer.com") > -1) {
    var query = mUrl.substring(mUrl.indexOf('?') + 1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == "oauth_verifier") {
    console.debug("authorized!!!");
    authorized = true;
    code = pair[1];
    }
    }
    }
    if (authorized) {
    requestPermanentToken(code);
    }
    console.debug("End: urlChanged()");
    }

    function requestPermanentToken(code) {
    console.debug("Start: requestPermanentToken()");
    var req = new XMLHttpRequest();

        req ()
            {
                if (req.readyState == XMLHttpRequest.DONE){
                    console.debug ("ResponseText: " + req.responseText);
                    snaprOAuth.state = "AuthDone";
    
                    var vars = req.responseText.split("&");
                    console.debug("Call authorize url: " + loginView.url);
                    var pair = vars[0].split("=");
                    oauth_token = pair[1];
                    var pair = vars[1].split("=");
                    oauth_token_secret = pair[1];
    
                    var db = openDatabaseSync("Token", "1.0", "the token", 1);
                    var dataStr = "INSERT INTO Token VALUES(?)";
                    var data = ["oauth_consumer_key=V7DkxSPVVX1u9z8JrBsdNw&oauth_token=" + oauth_token + "&oauth_signature_method=PLAINTEXT&oauth_timestamp=1297383841092&oauth_nonce=1047685618&oauth_signature=zoQnHIDOijk6PN4pBVFs4dFxjdJpQqG2NnWB6sWK0Q&" + oauth_token_secret];
                    db.transaction(function(tx) {
                        tx.executeSql('CREATE TABLE IF NOT EXISTS Token(token TEXT)');
                        tx.executeSql(dataStr, data);
                    })
                }
            }
    
        console.debug("URL: " + "https://www.yammer.com/oauth/access_token?oauth_consumer_key=V7DkxSPVVX1u9z8JrBsdNw&oauth_token=" + oauth_token + "&oauth_signature_method=PLAINTEXT&oauth_timestamp=1297383841092&oauth_nonce=1047685618&oauth_verifier=" + code + "&oauth_signature=zoQnHIDOijk6PN4pBVFs4dFxjdJpQqG2NnWB6sWK0Q&" + oauth_token_secret);
        req.open("GET", "https://www.yammer.com/oauth/access_token?oauth_consumer_key=V7DkxSPVVX1u9z8JrBsdNw&oauth_token=" + oauth_token + "&oauth_signature_method=PLAINTEXT&oauth_timestamp=1297383841092&oauth_nonce=1047685618&oauth_verifier=" + code + "&oauth_signature=zoQnHIDOijk6PN4pBVFs4dFxjdJpQqG2NnWB6sWK0Q&" + oauth_token_secret);
        req.send();
        console.debug("End: requestPermanentToken()");
    

    }

    function checkToken() {
    console.debug("Start: checkToken()");
    var db = openDatabaseSync("Token", "1.0", "the token", 1);
    var dataStr = "SELECT * FROM Token";
    db.transaction(function(tx) {
    //tx.executeSql('DROP TABLE Token');
    tx.executeSql('CREATE TABLE IF NOT EXISTS Token(token TEXT)');
    var rs = tx.executeSql(dataStr);
    if (rs.rows.length > 0) {
    console.debug("AuthDone:" + rs.rows.item(0).token);
    snaprOAuth.token = rs.rows.item(0).token;
    snaprOAuth.state = "AuthDone";
    } else {
    console.debug("Call request_token");
    var req = new XMLHttpRequest();
    req.open("GET", "https://www.yammer.com/oauth/request_token?oauth_consumer_key=V7DkxSPVVX1u9z8JrBsdNw&oauth_signature_method=PLAINTEXT&oauth_timestamp=1297382941014&oauth_nonce=545746008&oauth_signature=zoQnHIDOijk6PN4pBVFs4dFxjdJpQqG2NnWB6sWK0Q&");
    req.send();
    req.onreadystatechange = function() {
    if ( req.readyState == XMLHttpRequest.DONE ) {
    console.debug ("ResponseText: " +req.responseText);
    var vars = req.responseText.split("&");
    console.debug("Call authorize url: " + loginView.url);
    var pair = vars[0].split("=");
    oauth_token = pair[1];
    var pair = vars[1].split("=");
    oauth_token_secret = pair[1];
    loginView.url = "https://www.yammer.com/oauth/authorize?" + vars[0] ;
    snaprOAuth.state = "Login";
    }
    }
    }
    });
    console.debug("End: checkToken()");
    }
    @



  • Part two: :-)

    OAuth.qml
    @
    import QtQuick 1.0
    import QtWebKit 1.0
    import Qt 4.7
    import "oauth.js" as OAuth

    Item {
    id: snaprOAuth

    property string token
    width: 200
    height: 200
    anchors.fill: parent
    Component.onCompleted: OAuth.checkToken()
    
    WebView{
        id: loginView
        anchors.fill: parent
        visible: false
        onUrlChanged: {
            console.log("URL Changed:" + url)
            OAuth.urlChanged(url)
        }
    }
    
    states: [
            State {
                name: "Login"
                PropertyChanges {
                    target: loginView
                    visible: true
                }
            },
            State {
                name: "AuthDone"
                PropertyChanges {
                    target: loginView
                    visible: false
                    opacity: 0
                }
    
                PropertyChanges {
                    target: snaprOAuth
                    visible: false
                    opacity: 0
                }
            }
        ]
    

    }
    @

    Main.qml
    @
    import QtQuick 1.0
    import QtWebKit 1.0

    Rectangle {
    id: screen
    width: 360
    height: 360

    OAuth{
        id: oauth
        z: 1
        onTokenChanged: {
                console.debug ("Token Changed")
                visible: 0
                xmllistmodel.source = "https://www.yammer.com/api/v1/messages.xml?"+ oauth.token
                xmllistmodel.reload()
            }
    
    }
    
    XmlListModel{
        id: xmllistmodel
        //source: "https://www.yammer.com/api/v1/messages.xml?"+ oauth.token
    
        query: "/response/messages/message"
        XmlRole { name: "messagetext"; query: "body/plain/string()" }
        XmlRole { name: "senderid"; query: "sender-id/string()" }
        onSourceChanged: {
            console.debug(source)
            console.debug(xmllistmodel.count)
        }
        onStatusChanged:{
            console.debug(status)
            console.debug(xmllistmodel.errorString())
        }
    }
    
    ListView{
        anchors.fill: screen
        id: list
        model: xmllistmodel
        delegate: MyListViewDelegate{}
    }
    

    }
    @

    thanks,
    Ayden



  • Hi all,
    any idea?

    thanks,
    ayden


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.