【问题标题】:Lua request from Tado thermostat api来自 Tado 恒温器 API 的 Lua 请求
【发布时间】:2021-01-18 00:05:19
【问题描述】:

我正在使用“Lua”编程语言在 Home Center 3(来自 Fibaro)中构建一个所谓的“Quickapp”。我想从 Tado api 获取一些数据,但它的记录很差。我不断从控制台收到以下消息:

未经授权访问此资源需要完全身份验证

我认为那是因为我需要从请求中分配 Bearer 令牌,但我有点不知所措......

这是我目前所拥有的:

function QuickApp:fetchTadoData(username,password,client_secret)


local url = "https://auth.tado.com/oauth/token"
  local postdata = {
        ["client_id"] = "tado-web-app",
        ["grant_type"] = "password",
        ["scope"] = "home.user",
        ["username"] = username, 
        ["password"] = password, 
        ["client_secret"] = client_secret
  }
  local extraheaders = {
        ["content-type"] = "application/json" 
  }
 
  self.http:request(url, {
    options={
      headers = extraheaders,
      data = json.encode(postdata),
      method = "POST"
    },
    success = function(status)
       self:debug(status.data)
    end,
    error = function(error)
       errorlog("Error getting data: "..error)
       self:debug("hallo")
    end
  })
end

我知道获取“不记名令牌”响应的 Curl 代码:

curl -s "https://auth.tado.com/oauth/token" -d client_id=tado-web-app -d grant_type=password -d scope=home.user -d username="you@example.com" -d password="Password123" -d client_secret=wZa

但我不知道如何将其翻译成上面的 Lua 代码。任何帮助表示赞赏!

【问题讨论】:

    标签: post lua token


    【解决方案1】:

    https://manuals.fibaro.com/home-center-3-quick-apps

    看起来不错,我注意到的主要是:

    "self.http 必须是之前由 net.HTTPClient 创建的"

    function QuickApp :fetchTadoData( username, password, client_secret )
      self .http = net .HTTPClient( { timeout = 5000 } )  --  5 seconds
    
      local url = "https://auth.tado.com/oauth/token"
     
      local requestBody = {
          action = 'create',
          params = {
              ["client_id"] = "tado-web-app",
              ["grant_type"] = "password",
              ["scope"] = "home.user",
              ["username"] = username, 
              ["password"] = password, 
              ["client_secret"] = client_secret
          }
      }
    
      local extraheaders = {
            ["content-type"] = "application/json",
            ["accept"] = "application/json" 
      }
    
      self .http :request( url, {
        options = {
          headers = extraheaders,
          data = json .encode( requestBody ),
          method = "POST"
        },
    
        success = function( response )
           self :debug( response .status )
           self :debug( response .data )
        end,  --  success
    
        error = function( msg )
           self :debug( 'Error: ' ..msg )
        end  --  error
      })
    end  --  :fetchTadoData()
    

    【讨论】:

      猜你喜欢
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      • 1970-01-01
      相关资源
      最近更新 更多