question

Lars Lindegaard Mikkelsen avatar image
Lars Lindegaard Mikkelsen asked

API - server error 401

I would like to access the API (https://vrmapi.victronenergy.com/v2/auth/login) from a C# application, but gets a server error 401. Any example avialable?

api dc system
2 |3000

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

6 Answers
Nikolay Manolov avatar image
Nikolay Manolov answered ·

Hey, perhaps if you provide C# code I could help you adjust it.

What you need to do according to the documentation, and which works for me, is:
1. Send a POST request to https://vrmapi.victronenergy.com/v2/auth/login with a a payload {"username":"your_user", "password":"your_pass"}
2. Receive back {"idUser":12345, "token": "random_token_characters"}
3. Include the token that you've received in as a header with every consequent request, for example https://vrmapi.victronenergy.com/v2/users/12345/installations, as follows
X-Authorization:"Bearer random_token_characters"

Regards,
Nik

2 comments
2 |3000

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

Lars Lindegaard Mikkelsen avatar image Lars Lindegaard Mikkelsen commented ·

Hi Nik,

Hereby the C# code - thanks in advance :-)


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

var client = new WebClient();

var method = "POST";

var parameters = new NameValueCollection();


parameters.Add("username", usernameVictron);

parameters.Add("password", passwordVictron);


var response_data = client.UploadValues(url, method, parameters);

var responseString = UnicodeEncoding.UTF8.GetString(data);

0 Likes 0 ·
Nikolay Manolov avatar image Nikolay Manolov Lars Lindegaard Mikkelsen commented ·

Hey Lars,

This code actually looks correct (although I can't test it right now).
In such a case my best guess for the 401 is that the user and/or pass are incorrect/invalid.

Perhaps test it in some other way. Postman is a really nice tool for dealing with APIs https://www.getpostman.com There's a browser extension for it as well. I guess just lookup some quick tutorial.
Or if you fancy the command line try curl or httpie, etc.


Regards,

Nik

0 Likes 0 ·
denzel avatar image
denzel answered ·

Hi Guys,


Im a Newby, Just got my Victron setup with the venus gx and now wanting to use the API to add data to my IOT home assistance dashboard .

Im using postman to test but all i get is :


{

"success": false,

"errors": "Invalid credentials",

"error_code": "invalid_credentials"

}


Posting to : https://vrmapi.victronenergy.com/v2/auth/login

{"username":"xxxxxx@xxxx.com", "password":"abcdef"}


"the username and password is correct as i tested it on the portal"

what am i doing wrong?


Regards

2 |3000

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

syrea avatar image
syrea answered ·

Same error here; I've got 401 "Invalid credentials". i've tried with postman and curl...

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.

syrea avatar image syrea commented ·

Ok, i've managed to solve my problem with postman. Don't use their Auth parameters but put your login info (JSON format) in the body (maybe not the best idea, but i've managed to login this way with postman)

1 Like 1 ·
dmmutch avatar image
dmmutch answered ·

Same problem here. Anyone got any further tips? Thanks

2 |3000

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

upsy avatar image
upsy answered ·

Same problem...

2 |3000

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

steve-smith avatar image
steve-smith answered ·

In case it helps someone
A C# program to login to the API

Just replace the username and password variableProgram.txt


program.txt (1.6 KiB)
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