Posted by mshrive3
Digon

Creating multiple new users using Python API scripts

I could use some help from a Python / TV API expert to help with debugging an issue I'm having with trying to add multiple new users to Team Viewer. 

I've adopted the sample python api scripts (ImportUserCsv.py, Common.py) to add new users to our Team Viewer account.  It works fine as long as I'm only adding one user.  However, when it tries to add more than one user, it hangs.   Similar to the sample scripts, my scripts loop through a list of emails and first looks up whether that email exists (GetUserByEmail).  If it does, it calls a function to update the user (UpdateUser) and if not, it calls a function to create the user (CreateUser). 

for id in idsToAdd :
  result = Common.GetUserByMail(id['email'])
  if len(result['users'][0]) > 0 :   # i.e. user was found
    user = result['users'][0]
    Common.UpdateUser(user['id'], id)
  else :     # need to add user
    Common.CreateUser(id, defaultefaultUserPermissions, defaultUserLanguage, defaultUserPassword)

As mentioned, with one user, it works fine, but more than one, it doesn't.  Specifically, it's the calls to UpdateUser or CreateUser that is causing the problem, because if I comment out those calls, it loops through everything successfully.  

There seems to be two different failure modes.  Either, it will throw an error after quite a long wait (~3 minutes). Or, it will hang and I need to stop the script.   The error is interesting in that, when it calls the UpdateUser or CreateUser, it will complain when looking the second user up:

Request [GET] https://webapi.teamviewer.com/api/v1/users?email=user2@company.com&full_list=true
Request failed! The error was:  HTTPSConnectionPool(host='webapi.teamviewer.com', port=443): Max retries exceeded with url: /api/v1/users?email=user2@company.com&full_list=true (Caused by ProxyError('Cannot connect to proxy.', RemoteDisconnected('Remote end closed connection without response',)))
Request failed! The error was:  local variable 'request' referenced before assignment

It seems like the UpdateUser function is holding onto the first connection and when going through the loop the second time, it's not able to lookup the second user. 

Please note, that because I'm on Python3 and behind a corporate firewall, I'm using the requests module, rather than httplib. 

I've tried adding a sleep for up to 10 seconds between user IDs, but that doesn't do anything other, than taking longer to see the failure mode. I've also tried closing the connection, but that also doesn't seem to do anything.   I'm not sure if I should focus on the behavior of the requests module, or whether this is something specific to the Team Viewer API.  

I can provide more information if needed. I hope there is someone out there that can help me try to some new things to debug. 

1 Reply
1 Reply
Posted by mshrive3
Digon

Re: Creating multiple new users using Python API scripts

Ok, with the help of a co-worker, the mystery is now solved.   In my implementation, I'm using a global variable dictionary called baseHeaders to set my authentication token.   In my UpdateUser and CreateUser functions, I thought I was creating a copy of that variable, by doing: 

headers = baseHeaders

However, in Python that does not create a copy, rather it is like a pointer to the original!  When I updated the local headers variable with additional info for generating the put request, it actually updated the global baseHeaders variable.  I made the following change, and it created a copy of the original baseHeaders global variable: 

headers = dict(baseHeaders)