Get Device Report JSON into python dictionary

Hello All

I have a Python script that successfully dopwnloads the devices report from
https://webapi.teamviewer.com/api/v1/devices

My problem begins when I try to get at individual fields in the repport theough a python dictionary; for example I want the vlaues of the remotecontrol_id, device_id, alias etc to go eventually into SQL Server. This is proving impossible as all these appear to be located as one huge string under the devices key. Is there anyway to do this?

Thanks

.

 

Best Answers

Answers

  • RlS
    RlS Posts: 15

     Hi DomLan

    Thanks

    Now getting - TypeError: the JSON object must be str, bytes or bytearray, not Response

     

    oh yes also the URL of the REST thing I originally put down is incorecct I am now using;

    webapi.teamviewer.com/api/v1/reports/devices"

     

    TypeError: the JSON object must be str, bytes or bytearray, not Response

     

    Im trying this

     

    headers = {"content-type": "application/json", "Authorization": "Bearer XXXXX"}
        response = requests.get(url, headers=headers)     
        all = json.loads(response)    
        for k,v in all.items():    
            print(r3['records'][0]['id'])
            print(r3['records'][1]['originid'])
            print(r3['records'][2]['userid'])

     

     

     

    Any ideas?

  • DomLan
    DomLan Posts: 490 ⭐Star⭐

    Hi @RlS 

    for JSON Response Content there's also a builtin JSON decoder, so

    import requests

    headers = {"content-type": "application/json", "Authorization": "Bearer XXXXX"}
    response = requests.get(url, headers=headers)
    all = response.json()
    for k,v in all.items():
    print(r3['records'][0]['id'])
    print(r3['records'][1]['originid'])
    print(r3['records'][2]['userid'])

     

    Domenico Langone

    MCSD: App Builder

  • RlS
    RlS Posts: 15

    Hi DornLan

    headers = {"content-type": "application/json", "Authorization": "Bearer "xxxx"}
    response = requests.get(url, headers=headers)
    all = response.json()
    Throws:
    print(all['records'][0]['id'])
    KeyError: 'id'

    It can't see 'id'. It looks as if there is only one key 'records' all the others are in one long string under the records key? Was this planned? why would teramviewer do thid? has anybody got this to work in python 3.7, or should I be using something other language?

    thanks

  • DomLan
    DomLan Posts: 490 ⭐Star⭐

    Sorry, I've copied all from previous reply.

    Root is devices and field device_id -> print all['devices'][0]['device_id']

    Try here!

    Domenico Langone

    MCSD: App Builder

  • DomLan
    DomLan Posts: 490 ⭐Star⭐

    Hi @RlS 

    I don't think so: I feel I can say, as a user, that this is one of the most accurate documentation I've seen over time

    DocTeamViewerDevices01.pngDocTeamViewerDevices02.png

    You are probably referring to problems of homogeneity in the definition of the type of data returned: yes, there are some. But technically they are all justifiable, somehow, and I don't think it's worth reporting them. In any case, a change at this level would generate a breaking-change that is difficult to justify.

    The documentation of every single call and its answer must always be kept in hand.

    Regards

    Domenico Langone

    MCSD: App Builder

  • RlS
    RlS Posts: 15

    Yes, I was just confused by the reference to /reports in the URL. Anyway, when you run this do you get all the devices in the JSON or just one line at a time wirhthat code. All I'm getting is one line at a time?

  • DomLan
    DomLan Posts: 490 ⭐Star⭐

    Hi @RlS,

    If you agree, I would say that your initial question has been resolved. I believe it is correct to follow the response validation mechanism, once your query has found a correct solution or at least your initial problem has been solved.

    In your case it also applies to other threads you have started on the same topic, which have been answered, but we don't know if they helped you solve your problem.

    You have started this thread by saying: "I have a Python script that successfully dopwnloads the devices report from https://webapi.teamviewer.com/api/v1/devices"

    What I can understand is:
    - you have no problem
    - you know you are working with an array of results (if you get only one it's because you set a filter on request)
    - the term /report in the address has never been mentioned.

    In order that it does not become a conversation limited to a specific case that deals with potentially different problems, I suggest you start a new thread, maybe more specific and in which you provide a greater number of details about the lines of code you use (obviously you don't have to report codes that allow others to trace your authentication! remains generic)

    I think a good way to solve the problem you're encountering is to divide it into smaller problems ... and in any case, the first answer that was given to you in this thread I think was the correct one.

    Regards

    Domenico Langone

    MCSD: App Builder

  • RlS
    RlS Posts: 15

    Thank you, I will start a new thread