question

cdsahin avatar image
cdsahin asked

API - MATLAB Error

Hi Eveyone,


I have been trying to access the API data from MATLAB. However, I get en error of which screenshot is below. I am sure my credentials are correct.

Has anyone ever tried to pull the API data from MATLAB using RESTful request-response model?


VRM
1580825773980.png (43.0 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.

1 Answer
Teun Lassche avatar image
Teun Lassche answered ยท

Hi! Make sure that you write a JSON object, see the โ€˜Write JSON objectโ€™ section here: https://nl.mathworks.com/help/matlab/ref/webwrite.html

3 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.

cdsahin avatar image cdsahin commented ยท

Hi Teun,

Thank you. It worked! I managed to receive the 'token' and 'idUser'.

However, this time, I have another issue regarding using the 'token' and 'idUser' information to pull the API data. Do you have any suggestion about how it can be done? Somehow, I should find a way to use the 'token' and 'idUser'.

Here are my code and error message:

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

data = jsonencode(struct('username','***','password','***'))

response = webwrite(url,data)

headerFields = {'Authorization', ['Bearer ', response.token]};
options = weboptions('HeaderFields', headerFields, 'ContentType','json');

url2 = ('https://vrmapi.victronenergy.com/v2/users/'+ string(response.idUser) +'/installations')

resp2 = webwrite(url2,options);

0 Likes 0 ยท
1580869010873.png (14.3 KiB)
1580869296248.png (14.2 KiB)
Teun Lassche avatar image Teun Lassche โ™ฆ cdsahin commented ยท

It's been a while since I did a lot of matlab, but I think you should do it like this:


headerFields = {'Authorization', ['Bearer ' response.token]};
options = weboptions('HeaderFields', headerFields, 'ContentType','json');

url2 = ('https://vrmapi.victronenergy.com/v2/users/'+ string(response.idUser) +'/installations')

resp2 = webwrite(url2,[], options);

Note the subtle changes in the bearer array and the webwrite call arguments.

0 Likes 0 ยท
ldoppler avatar image ldoppler Teun Lassche โ™ฆ commented ยท

I just needed to retrieve data from the VRM using Matlab recently. The following code is working right. In the code above "X-" was missing before "Authorization" in the header fields and the webwrite works with and without [] as second argument of the webwrite

url = 'https://vrmapi.victronenergy.com/v2/auth/login';
data = jsonencode(struct('username', username,'password',password));
response = webwrite(url,data);

headerFields = {'X-Authorization', ['Bearer ' response.token]};
options = weboptions('HeaderFields', headerFields, 'ContentType','json');
url2 = ('https://vrmapi.victronenergy.com/v2/users/'+ string(response.idUser) +'/installations');

resp2 = webwrite(url2, options);


0 Likes 0 ยท