如何使用Ubuntu Touch应用程序中的OAuth?

我正在用QML编写一个Ubuntu Touch应用程序。 我想与Trello整合。 有两种方法可以在API中登录Trello,其中一种是我计划使用的OAuth。 从QML中做到这一点的最佳方法是什么? 我不想使用C ++后端,但如果这是唯一的方法,我愿意这样做。

您可以为Trello创建一个帐户插件,以便可以从“系统设置”中的“在线帐户”面板创建Trello帐户。 您可以使用Ubuntu.OnlineAccounts QML模块登录,如下所示:

import QtQuick 2.0 import Ubuntu.OnlineAccounts 0.1 Rectangle { width: 400 height: 300 AccountServiceModel { id: accounts service: "trello-board" } ListView { id: listView anchors.fill: parent model: accounts delegate: Item { width: parent.width height: 60 AccountService { id: accts objectHandle: accountServiceHandle onAuthenticated: { console.log("Access token is " + reply.AccessToken) } onAuthenticationError: { console.log("Authentication failed, code " + error.code) } } Text { anchors.fill: parent text: providerName + ": " + displayName MouseArea { anchors.fill: parent onClicked: accts.authenticate(null) } } } } } 

此代码将为您提供OAuth令牌。 要首先创建帐户,您需要创建以下文件:

  • /usr/share/accounts/providers/trello.provider
  • /usr/share/accounts/services/trello-board.service
  • /usr/share/accounts/qml-plugins/trello/Main.qml

鉴于Trello使用OAuth 1.0,如Flickr和twitter,只需使用twitter或flickr版本作为模板创建上述文件,并根据需要进行修改(对于.service文件,您可以使用flickr-sharing.service sharing.service); 在trello.provider您需要更改API端点,如下所示:

 https://trello.com/1/OAuthGetRequestToken https://trello.com/1/OAuthGetAccessToken https://trello.com/1/OAuthAuthorizeToken 

当然,更改其他字段(回调URL,客户端ID和密码)以匹配您在使用Trello注册应用时设置的字段。 如果一切顺利,您将能够从系统设置中的“在线帐户”面板创建Trello帐户。

由于mardy使用的方法实际上并不适用于Ubuntu Touch上受限制的应用程序,因此有必要让OAuth自己跳舞。 实质上,您需要在WebView加载登录页面,然后使用onUrlChanged信号拦截响应以提取身份validation令牌。 下面是使用StackExchange自己的OAuth实现的示例。

OAuth.qml

 import QtQuick 2.0 import QtWebKit 3.0 import "OAuth.js" as OAuth Rectangle { height: 750 width: 500 Text { id: nextState visible: false anchors.centerIn: parent text: "Log in successful!" } Item { id: stackOAuth property string nextState: "AuthDone" anchors.fill: parent Component.onCompleted: OAuth.checkToken() property string token: "" WebView { id: loginView visible: false anchors.fill: parent onUrlChanged: OAuth.urlChanged(url) } Rectangle { height: 50 width: parent.width anchors.left: parent.left anchors.right: parent.right anchors.bottom: parent.bottom Text { text: loginView.url } } states: [ State { name: "Login" PropertyChanges { target: loginView visible: true url: "https://stackexchange.com/oauth/dialog"+ "?redirect_uri=https://stackexchange.com/oauth/login_success"+ "&client_id=YOUR_CLIENT_ID&scope=read_inbox" } }, State { name: "AuthDone" PropertyChanges { target: loginView visible: false opacity: 0 } PropertyChanges { target: nextState visible: true } } ] } } 

然后在OAuth.js您有代码从URL中提取令牌并且handel从您的数据库中存储/检查它:

 .import QtQuick.LocalStorage 2.0 as Sql function urlChanged(url) { var authorized = false; var mUrl = url.toString(); var token = ""; if (mUrl.indexOf("https://stackexchange.com") > -1) { var query = mUrl.substring(mUrl.indexOf('#') + 1); var vars = query.split("&"); for (var i=0;i 

这个例子(或多或少)是从诺基亚到QtQuick 2.0的旧QtQuick 1.0示例的一个端口。

    Interesting Posts