Posted by esteven
Henagon

Teamviewer API documentation

Hello;

I'm new to Teamviewer API interface and I find the documentation quite bad since there are no code examples anywhere regarding javascript. I need the code examples from the functions in all the sections within the API documentation, so know what I'm doing wrong.

I'm getting error 405 all the time, even for the ping request. If I don't set any header the request ends correctly, but if I add the

request.setRequestHeader('Authentication, '4354208-XXXXXXXXXXXXXXXXX');

I end having a nice 405 error along with this error:

Failed to load https://webapi.teamviewer.com/api/v1/ping: Response for preflight does not have HTTP ok status.

Can someone help giving some extra documentation on complete javascript (not jquery) function examples on how to do a couple of things? this is the first API documentation I see in my life without any example inside :(.

Thanks.

8 Replies
Posted by DomLan
Heptagon

Re: Teamviewer API documentation

Hi @esteven,

sorry for late. Try to add "Bearer" in value for authentication (and Authorization for header name!)

request.setRequestHeader('Authorization', 'Bearer 4354208-XXXXXXXXXXXXXXXXX');


Personally I believe that the documentation supplied with the TeamViewer API is one of the best available for REST functionalities. Trust me, as a technician I am very demanding on the subject.

Trust me:

27-09-2018 17-13-45.png

In any case 405 states for Method Not Allowed. You're probably using the verb GET instead of a POST. In any case, if you tell me the flow of operations you want to perform I can show you the correct path. Then make the steps in JS rather than in C # or Ruby is the same thing.

Let me know if you find more problems and if I can help you.

Regards. 

Domenico Langone
MCSD: App Builder
Posted by esteven
Henagon

Re: Teamviewer API documentation

Wow, I'm eager to try your solution.

I already tried it without Bearer first but no avail, and reading in the forums before posting my issue taught me to add the Bearer, but I still couldn't manage to make it work. So, thanks to you I may have noticed what is wrong in my request, 'Authentication' argument.

I will try it on monday once I get to the office, since I need to continue building the mini panel for our company.

Thanks!

Posted by esteven
Henagon

Re: Teamviewer API documentation

Ok, the change in the header didn't work, it shows the same issue, error 405.

My complete request is as follows:

function getdevices()
	{
	var request = new XMLHttpRequest();
	var method = "POST";
	var url = "https://webapi.teamviewer.com/api/v1/devices";

	request.onreadystatechange = function() {
		if (this.readyState == XMLHttpRequest.DONE && this.status == 200)
			{
			console.log("The request was successfully done");
			}
		}
	request.open(method, url, true);
	request.setRequestHeader('Authorization', 'Bearer 4353785-XXXXXXXXXXXXXXX');
	request.send();
	}

I've tried changing GET method into POST but no avail, it does not work anyways.

As a side information, I tried the ping request and it works with the same structure, but only when no setRequestHeader is sent to the request. Very strange.

Let's see where I'm being mistaken :)

Thanks!

Posted by DomLan
Heptagon

Re: Teamviewer API documentation

Hi @esteven,

before starting... a few questions: they do not serve to understand your degree of competence, but to modulate the terms that I will use in the next answer.


You are switching from an initial PING to adding a DEVICE to an account: have you read the whole manual?


I have not yet understood the authentication process followed, but I try to hypothesize that it is a question from a SCRIPT and non for APP authorization code. It's correct? Is your code  for User access or for Company access?


In your POST I do not see references to the body release: you are running a POST then an addition of DEVICE, but all references are missing to load this device. Excuse the repetition, did you download the manual?

You called your GetDevices function ... I guess you want to read the whole set of devices connected to the account represented by that authorization code used in the call. If you want to read and do not write you should use GET instead of POST.
Keep this in mind:
GET: read
POST: add
PUT: change
DEL: cancel
PATCH: targeted modification (you will not see it in the TeamViewer REST set)

Let me know.

Regards

Domenico Langone
MCSD: App Builder
Posted by esteven
Henagon

Re: Teamviewer API documentation

Ok, I had to put more info here:

I use token access Company type, with permissions to do everything in order to test it.

The function is not for adding a device, I wrote POST but the initial method was GET.

I don't know what do you mean by the "body release". Let's switch the POST with GET, so why I'm getting an error? I want the request response contains all devices in the company.

In the documentation I see:

GET /api/v1/devices (list all devices from the computers & contacts list)
Parameters
•online_state(optional) – Return only devices with the given online_state.
•groupid(optional) – Return onlydevicesthat are in the specified group.
•remotecontrol_id – Return only devices with the specified remotecontrol_id

 

What am I missing in the code?

The purpose of what I'm doing is to batch view, edit and delete things (users and devices mainly), since the panel surprisingly does not allow the user to upload csv's to do the task.

Highlighted
Posted by Supertramp
Trigon

Re: Teamviewer API documentation

Hi @esteven,

so... "body release"... this is my translation error. It is the body of an HTTP request. Since you perform a post the parameters are exchanged in the body.
Obviously what you want to perform is a reading, then a GET, so the parameters must be exchanged in the query string. I hope they are words that you know.

From manual (page 6):

The TeamViewer API is a REST API which uses the already existing HTTP methods to create (
POST), read (GET), change (PUT) or delete (DELETE) single items or a collection of items. The following table shows the
general use cases for these HTTP methods.
....
The basic URI scheme for all API functions is: https://host/path/to/resources[/id][/verb][?param1=value1]
The TeamViewer API can be found at https://webapi.teamviewer.com/
Parameters in the URI are only allowed for GET and DELETE. Generally there should be
no need for any parameters for DELETE, though. POST and PUT need to have the parameters in the body formatted as JSON or XML.

To make it easy:

function getdevices()
      {
      var request = new XMLHttpRequest();
      var method = "GET";
      var url = "https://webapi.teamviewer.com/api/v1/devices";
      var params = 'online_state=xxxx&groupid=yyyy&remotecontrol_id=kkkk'

      request.onreadystatechange = function() {
            if (this.readyState == XMLHttpRequest.DONE && this.status == 200)
                  {
                  console.log("The request was successfully done");
                 }
            }
      request.open(method, url, true);
      request.setRequestHeader('Authorization', 'Bearer 4353785-XXXXXXXXXXXXXXX');
      request.send(params);
      }

Obviously all the parameters are not necessary, on the contrary the only GET would suffice to the address you recovered ... but... To make it more easy... From manuale (page 51 DEVICES/GET)

Authentication -> User access token. Scope: ContactList.Read.

So --> Your authorization token is not suitable for the purpose.

Can we change perspective? Show me the scenario you want to create and what are the elements at your disposal? Would you be able to switch to a management that requires authentication via OAuth to TeamViewer?

Let me know.

Regards

 

 

The real programmer does not interpret dreams. Just compile them
2 Replies
Posted by esteven
Henagon

Re: Teamviewer API documentation

Impossible. The function does not work. I used as parameter:

var parameters = "remotecontrol_id=XXXXXXX";

This refuses of working no matter what. This is not the first time I use ajax requests for different stuff and this is the first time I encounter such a stubborn API.

Since I'm sure I'm not the only one being in the same situation, let's try to use this thread as a guide for other people, so they don't need to look unsuccessfully throughout all internet searching examples on functions to make it working.

My current function is as follows:

function getdevices()
	{
	var request = new XMLHttpRequest();
	var method = "GET";
	var url = "https://webapi.teamviewer.com/api/v1/devices";
	var parameters = "remotecontrol_id=XXXXXXXXX";

	request.onreadystatechange = function() {
		if (this.readyState == XMLHttpRequest.DONE && this.status == 200)
			{
			console.log("The request was successfully done");
			}
		};
	request.open(method, url, true);
	request.setRequestHeader('Authorization', 'Bearer 4390055-XXXXXXXXXXXXXX');
	request.send(parameters);
	}

The token has been recreated for User scope, with all the permissions for testing.

I assume the parameters variable is correct, since the documentation says so, but the error is:

teamviewer.js:33 OPTIONS https://webapi.teamviewer.com/api/v1/devices 405
getdevices @ teamviewer.js:33
onclick @ index.html:25
index.html:1 Failed to load https://webapi.teamviewer.com/api/v1/devices: Response for preflight does not have HTTP ok status.

The scenario is:

I'm creating a local control panel, hosted in a local apache within the company.

The control panel will contain some functions that will do batch tasks. Each task is one function that will be added to the code, and will be triggered through a button.

The conditions are:

  • Vanilla javascript should be used no matter what.
  • No oAuth, only token.
  • Panel must be hosted locally.

Let's see if we can find out what's wrong in this mess :)

Posted by DomLan
Heptagon

Re: Teamviewer API documentation

Hi @esteven,

thank you for describing your scenario. If you've already been able to use other client-side APIs, you should already have clashed with the CORS mechanism. If you try to sniff your request with special tools (Fiddler?) You will notice that the browser changes your initial GET (or POST) in an OPTIONS request to which the TeamViewer server responds with some Access-Control-Allow-x data. This type of response does not allow you, on the client side, to continue the conversation. This is a very strict (browser) security policy and has often been very difficult to overcome even for testing purposes.

It should be easier for you to build something server side, acting as proxy for requests to TeamViewer APIs. You can use Node.js to keep JavaScript as programming language or use the one in use for your web application server side (PHP?, C#, other?).

I'm sorry, I have no other suggestions for you.

Have a nice day.

Domenico Langone
MCSD: App Builder