content: sub init()
      m.title = m.top.findNode("title")
      m.instructions = m.top.findNode("instructions")
      m.authUrl = m.top.findNode("authUrl")
      m.codeLabel = m.top.findNode("codeLabel")
      m.linkingCode = m.top.findNode("linkingCode")
      m.status = m.top.findNode("status")
      m.cancelButton = m.top.findNode("cancelButton")
      
      m.cancelButton.observeField("buttonSelected", "onCancelPressed")
      
      ' Start the linking process
      startDeviceLinking()
  end sub
  
  sub startDeviceLinking()
      ' Create device linking request
      request = CreateObject("roUrlTransfer")
      request.setUrl("https://api.put.io/v2/oauth2/device")
      request.setRequest("POST")
      request.addHeader("Content-Type", "application/x-www-form-urlencoded")
      
      ' TODO: Replace with your actual Put.io OAuth Client ID
      ' Get this from: https://app.put.io/oauth
      clientId = "YOUR_PUTIO_CLIENT_ID_HERE"
      postData = "client_id=" + clientId + "&response_type=device_code"
      
      port = CreateObject("roMessagePort")
      request.setMessagePort(port)
      request.setPort(port)
      
      if request.asyncPostFromString(postData)
          m.status.text = "Requesting device code..."
          
          ' Wait for response
          msg = wait(10000, port)
          if msg <> invalid and type(msg) = "roUrlEvent"
              responseCode = msg.getResponseCode()
              responseString = msg.getString()
              
              if responseCode = 200
                  response = ParseJson(responseString)
                  if response <> invalid
                      m.linkingCode.text = response.user_code
                      m.deviceCode = response.device_code
                      m.interval = response.interval
                      m.expiresIn = response.expires_in
                      
                      ' Start polling for completion
                      startPolling()
                  else
                      m.status.text = "Error: Invalid response from Put.io"
                  end if
              else
                  m.status.text = "Error: Failed to get device code"
              end if
          else
              m.status.text = "Error: Request timeout"
          end if
      else
          m.status.text = "Error: Failed to start authentication"
      end if
  end sub
  
  sub startPolling()
      m.pollTimer = CreateObject("roSGNode", "Timer")
      m.pollTimer.duration = m.interval
      m.pollTimer.repeat = true
      m.pollTimer.observeField("fire", "pollForCompletion")
      m.pollTimer.control = "start"
      
      m.status.text = "Waiting for authentication..."
  end sub
  
  sub pollForCompletion(event as Object)
      request = CreateObject("roUrlTransfer")
      request.setUrl("https://api.put.io/v2/oauth2/device/token")
      request.setRequest("POST")
      request.addHeader("Content-Type", "application/x-www-form-urlencoded")
      
      ' TODO: Replace with your actual Put.io OAuth Client ID
      clientId = "YOUR_PUTIO_CLIENT_ID_HERE"
      postData = "client_id=" + clientId + "&device_code=" + m.deviceCode + "&grant_type=device_code"
      
      port = CreateObject("roMessagePort")
      request.setMessagePort(port)
      
      if request.asyncPostFromString(postData)
          msg = wait(5000, port)
          if msg <> invalid and type(msg) = "roUrlEvent"
              responseCode = msg.getResponseCode()
              responseString = msg.getString()
              
              if responseCode = 200
                  response = ParseJson(responseString)
                  if response <> invalid and response.access_token <> invalid
                      ' Authentication successful
                      m.pollTimer.control = "stop"
                      m.status.text = "Authentication successful!"
                      
                      ' Get user info
                      getUserInfo(response.access_token)
                  end if
              else if responseCode = 400
                  ' Still waiting for user to authenticate
                  ' Continue polling
              else
                  ' Error occurred
                  m.pollTimer.control = "stop"
                  m.status.text = "Authentication failed"
              end if
          end if
      end if
  end sub
  
  sub getUserInfo(accessToken as String)
      request = CreateObject("roUrlTransfer")
      request.setUrl("https://api.put.io/v2/account/info")
      request.addHeader("Authorization", "Bearer " + accessToken)
      
      port = CreateObject("roMessagePort")
      request.setMessagePort(port)
      
      if request.asyncGetToString()
          msg = wait(5000, port)
          if msg <> invalid and type(msg) = "roUrlEvent"
              responseCode = msg.getResponseCode()
              responseString = msg.getString()
              
              if responseCode = 200
                  response = ParseJson(responseString)
                  if response <> invalid and response.info <> invalid
                      ' Authentication complete
                      authData = {
                          success: true,
                          accessToken: accessToken,
                          user: response.info
                      }
                      m.top.authComplete = authData
                  end if
              end if
          end if
      end if
  end sub
  
  sub onCancelPressed(event as Object)
      if m.pollTimer <> invalid
          m.pollTimer.control = "stop"
      end if
      
      authData = {
          success: false,
          error: "User cancelled"
      }
      m.top.authComplete = authData
  end sub
  
  function onKeyEvent(key as String, press as Boolean) as Boolean
      handled = false
      
      if press
          if key = "back" or key = "escape"
              onCancelPressed(invalid)
              handled = true
          end if
      end if
      
      return handled
  end function
  