Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. XmlListModel - Host requires authentication
Forum Updated to NodeBB v4.3 + New Features

XmlListModel - Host requires authentication

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 4.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aydens
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      parancibia
      wrote on last edited by
      #2

      Can you please post a code snippet of your code?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aydens
        wrote on last edited by
        #3

        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()");
        }
        @

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aydens
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            aydens
            wrote on last edited by
            #5

            Hi all,
            any idea?

            thanks,
            ayden

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved