question

offgridtas avatar image
offgridtas asked

Help with API

Hi.


I'm trying to extract some SoC information from the Cerbo using a Raspberry Pi running Python. I'm able to get a token but when I try and use the token it is returning "errors":"Login required","error_code":"invalid_credentials"


Surely if it's giving me a token, the credentials aren't invalid.


Any help greatly appreciated.


  1. import requests
  2.  
  3.  
  4.  
  5.  
  6.  
  7. USERNAME = 'my username'
  8.  
  9. PASSWORD = 'my password'
  10.  
  11.  
  12.  
  13. # VRM API endpoints
  14.  
  15. LOGIN_URL = 'https://vrmapi.victronenergy.com/v2/auth/login'
  16.  
  17. DATA_URL_TEMPLATE = 'https://vrmapi.victronenergy.com/v2/installations/{installation_id}/stats/latest'
  18.  
  19.  
  20.  
  21. def get_auth_token(username, password):
  22.  
  23. # Authenticate and obtain an authorization token
  24.  
  25. headers = {'Content-Type': 'application/json'}
  26.  
  27. payload = {'username': username, 'password': password}
  28.  
  29. response = requests.post(LOGIN_URL, headers=headers, json=payload)
  30.  
  31.  
  32.  
  33. if response.status_code == 200:
  34.  
  35. token = response.json().get('token')
  36.  
  37. return token
  38.  
  39. else:
  40.  
  41. print(f'Failed to authenticate: {response.status_code} - {response.text}')
  42.  
  43. return None
  44.  
  45.  
  46.  
  47. def get_current_soc(token, installation_id):
  48.  
  49. # Get current State of Charge (SoC) using the obtained token
  50.  
  51. headers = {
  52.  
  53. 'Authorization':f'Bearer {token}',
  54.  
  55. 'Content-Type':'application/json'
  56.  
  57. }
  58.  
  59. url = DATA_URL_TEMPLATE.format(installation_id=installation_id)
  60.  
  61. response = requests.get(url, headers=headers)
  62.  
  63.  
  64.  
  65. if response.status_code == 200:
  66.  
  67. data = response.json()
  68.  
  69. soc = data.get('battery', {}).get('stateOfCharge')
  70.  
  71. if soc is not None:
  72.  
  73. return soc
  74.  
  75. else:
  76.  
  77. print('State of Charge (SoC) not found in response.')
  78.  
  79. return None
  80.  
  81. else:
  82.  
  83. print(f'Failed to fetch SoC: {response.status_code} - {response.text}')
  84.  
  85. return None
  86.  
  87.  
  88.  
  89. if __name__ == '__main__':
  90.  
  91.  
  92. installation_id = 'my installation ID'
  93.  
  94.  
  95.  
  96. # Authenticate and get token
  97.  
  98. token = get_auth_token(USERNAME, PASSWORD)
  99.  
  100. if token:
  101.  
  102. # Get current SoC
  103.  
  104. print(token)
  105.  
  106. soc = get_current_soc(token, installation_id)
  107.  
  108. if soc is not None:
  109.  
  110. print(f'Current State of Charge (SoC): {soc}%')
  111.  
  112. else:
  113.  
  114. print('Failed to fetch State of Charge (SoC)..')
  115.  
  116. else:
  117.  
  118. print('Authentication failed.')



api
1 comment
2 |3000

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

0 Answers

Related Resources

Additional resources still need to be added for this topic