question

javierag avatar image
javierag asked

VRM API Error 401 - invalid token

Hi!

I am working on a small project where I try to connect to a Victron Venus GX using VRM API. My code (in Python) is the following:

import requests

import json

login_url = 'https://vrmapi.victronenergy.com/v2/auth/login'

username = "aaaaa@bbbb.com"

password = "123456"

login_string = '{"username":"'+username+'","password":"'+password+'"}'

response = requests.post(login_url, login_string)


token = json.loads(response.text)["token"]


idSite = "1111111111"

diags_url = 'https://vrmapi.victronenergy.com/v2/installations/'+idSite+'/diagnostics'

querystring = {"count":"1000"}

headers = {

"Content-Type": "application/json",

"x-authorization": "Bearer <"+token+">"

}

response = requests.request("GET",diags_url, headers=headers, params=querystring)

print(response.json)

print(response.text)


I try this script but the response I get is like:

<bound method Response.json of <Response [401]>>

{"success":false,"errors":"Token is invalid","error_code":"invalid_token"}


Any idea and/or can someone support me?

Many thanks in advance

JavierAG

errorapi
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

2 Answers
Guy Stewart (Victron Community Manager) avatar image
Guy Stewart (Victron Community Manager) answered ·

Hi @JavierAG

There are quite a few existing posts regarding 401 API errors, some containing good info.

If you have already checked them, please include that so the existing solutions can be ruled out


2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

javierag avatar image
javierag answered ·

Hi,

Thanks for the advice, I could find some errors on my code. I add the new code with some insteresting changes:

import requests

import json

loginUrl = "https://vrmapi.victronenergy.com/v2/auth/login"

username = "aaaaa@bbbb.com"

password = "123456"

loginString = "{\"username\":\""+username+"\",\"password\":\""+password+"\"}"

responseLogin = requests.post(loginUrl, loginString)

dataLoginRequest = json.loads(responseLogin.text)

token = dataLoginRequest["token"]

idUser = str(dataLoginRequest["idUser"])

instUrl = "https://vrmapi.victronenergy.com/v2/users/"+idUser+"/installations"

#querystring = {"count":"1"}

headersInst = {

"Content-Type": "application/json",

"x-authorization": "Bearer "+token

}

responseInst = requests.get(instUrl, headers=headersInst)

print(responseInst)

print(responseInst.text)

responseLogin = requests.post(loginUrl, loginString)

dataLoginRequest = json.loads(responseLogin.text)

token = dataLoginRequest["token"]

dataInst = json.loads(responseInst.text)

if dataInst["success"]:

for installation in dataInst["records"]:

idSite = str(installation["idSite"])

print(idSite)

diagsUrl = "https://vrmapi.victronenergy.com/v2/installations/"+idSite+"/diagnostics"

querystringDiag = {"count":"1000"}

headersDiag = {

"Content-Type": "application/json",

"x-authorization": "Bearer "+token

}

responseDiag = requests.request("GET",diagsUrl, headers=headersDiag, params=querystringDiag)

print(responseDiag)

print(responseDiag.text)

Many thanks for the advices again

JavierAG

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

Related Resources

Additional resources still need to be added for this topic