Search code examples
jsonvb.netrestteamviewer

Get /Put Device Alias using Teamviewer REST API


Im looking to get a list of all the devices on my work admin teamviewer account using vb.net. I would also like to be able to change the "Alias" of a given device using it's device id. i know very little about API's. i found the following example but i am not sure how to adapt it to get the json response.

instead of the accesstoken, i believe i need to use the client id and secret id along with the authorization code in order to use this. if i run it in it's current start i get a 401 unauthorized error. Any help would be appreciated.

i also have no idea how to use "PUT" to change the Alias using the device id which will both be entered in textboxes. ex alias = textbox1.text and device_id = textbox2.text

Private Sub SurroundingSub()
Dim accessToken As String = "xxxxxxxxxxxxxxxxxxx"
Dim apiVersion As String = "v1"
Dim tvApiBaseUrl As String = "https://webapi.teamviewer.com"
Dim address As String = tvApiBaseUrl & "/api/" & apiVersion & "/devices"

Try
    Dim request As HttpWebRequest = TryCast(WebRequest.Create(address), HttpWebRequest)
    request.Headers.Add("Bearer", accessToken)
    request.Method = "GET"
    Dim webResp As WebResponse = request.GetResponse()
Catch __unusedException1__ As Exception
msgbox(__unusedException1__.ToString)
End Try
End Sub

Solution

  • Here is the Get all devices code:

    Private Sub get_teamviewer_devices()
        Dim accessToken As String = "XXXXXXXXXXXXXXXXXXXXX"
        Dim apiVersion As String = "v1"
        Dim tvApiBaseUrl As String = "https://webapi.teamviewer.com"
        Dim address As String = tvApiBaseUrl & "/api/" & apiVersion & "/devices"
        Dim result_json As String = Nothing
        Try
            Dim request As HttpWebRequest = TryCast(WebRequest.Create(address), HttpWebRequest)
            request.Headers.Add("Authorization", "Bearer " & accessToken)
            request.Method = "GET"
            Dim webResp As WebResponse = request.GetResponse()
            Using reader = New StreamReader(webResp.GetResponseStream)
                result_json = reader.ReadToEnd()
            End Using
            TextBox1.Text = result_json
        Catch __unusedException1__ As Exception
            MsgBox(__unusedException1__.ToString)
        End Try
    End Sub
    

    Here is the PUT portion to change an alias:

    Public Sub change_alias(ByVal device_id As String, ByVal alias_str As String)
        Dim accessToken As String = "XXXXXXXXXXXXXXXXXXXXX"
        Dim apiVersion As String = "v1"
        Dim tvApiBaseUrl As String = "https://webapi.teamviewer.com"
        Dim address As String = tvApiBaseUrl & "/api/" & apiVersion & "/devices/" & device_id
        Dim result As String
        Dim alias_str_ As String = Chr(34) & alias_str & Chr(34)
        Try
            Dim request As HttpWebRequest = TryCast(WebRequest.Create(address), HttpWebRequest)
            request.Headers.Add("Authorization", "Bearer " & accessToken)
            request.Method = "PUT"
            request.ContentType = "application/json"
            Using requestWriter2 As New StreamWriter(request.GetRequestStream())
                requestWriter2.Write("{""Alias"" : " & alias_str_ & "}")
            End Using
            Dim webResp As WebResponse = request.GetResponse()
            Using reader = New StreamReader(webResp.GetResponseStream)
                result = reader.ReadToEnd()
            End Using
            TextBox1.Text = (result)
        Catch __unusedException1__ As Exception
            MsgBox(__unusedException1__.ToString)
        End Try
    End Sub