Get 401 Unauthorized

Hi,

I want to use the /api/v1/devices part of the API of TeamViewer. I have creates my Script Token from the portal, and I am able to use the /api/v1/ping successfully. With the Ping function, I am getting a OK 200 response.

When trying to use the /api/v1/devices, I get the following in PowerShell:

"Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (401) Unauthorized."

This is my script:

$currentPath = Split-Path ((Get-Variable MyInvocation -Scope 0).Value).MyCommand.Path
. (Join-Path $currentPath "JSON 1.7.ps1")

$tvApiBaseUrl = "https://webapi.teamviewer.com"
$apiVersion = "v1"

$accesstoken = "XXXX-XXXXXXXXXXXXXXXXXX"

$req = [System.Net.WebRequest]::Create($tvApiBaseUrl + "/api/" + $apiVersion + "/devices")
$req.Method = "GET"
$req.Headers.Add("Authorization: Bearer $accessToken")

$res = $req.GetResponse()
$statusStr = $res.StatusCode
$statusCode = [int]$res.StatusCode

Write-Host "$statusCode $statusStr"

$resstream = $res.GetResponseStream()
$sr = new-object System.IO.StreamReader $resstream
$result = $sr.ReadToEnd()

if($statusCode -ne 200 )
{
Write-Host "Unexpected response code. Received content was:"
Write-Host $result
$result = $false
return $result
}

The script token have the following permission level:

Access level: Company
User managementView users
Session management: View all sessions
Group management: Read groups
Connection reporting: Read connection entries

Best Answer

Answers

  • christian-j
    christian-j Posts: 54 Staff member 🤠

    Hello holytoutant,

    your script is generally working.

    Are you using a user token?
    The company token will not work with this method.

    Please check the "error_description" within the response body to verify what's wrong.

    Christian

    Team Lead Product Development (Enterprise)

    Did my reply answer your question? To help others, please accept it as solution. Thanks!