added token generator and getting token from token.json

This commit is contained in:
Kim 2023-03-23 16:57:50 +01:00
parent 610aa05a38
commit 888a9f716b
3 changed files with 30 additions and 13 deletions

View File

@ -1,6 +1,5 @@
using System.Diagnostics; using System.Diagnostics;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using Flurl; using Flurl;
@ -138,7 +137,7 @@ public static class Program
//var installationName = await LookupInstallationNameByUniqueId(ccdName); //var installationName = await LookupInstallationNameByUniqueId(ccdName);
using var vrm = VrmAccount.Token(AccToken.Id, AccToken.Token); using var vrm = VrmAccount.Token(AccToken.idUser, AccToken.token);
var installations = await vrm.GetInstallations(); var installations = await vrm.GetInstallations();
var installationName = installations var installationName = installations
@ -155,7 +154,7 @@ public static class Program
{ {
Console.WriteLine($"looking up {ccdName} on VRM"); Console.WriteLine($"looking up {ccdName} on VRM");
using var vrm = VrmAccount.Token(AccToken.Id, AccToken.Token); using var vrm = VrmAccount.Token(AccToken.idUser, AccToken.token);
var installations = await vrm.GetInstallations(); var installations = await vrm.GetInstallations();
foreach (var installation in installations) foreach (var installation in installations)
@ -273,6 +272,11 @@ public static class Program
internal interface AccToken internal interface AccToken
{ {
public String Token { get; init; } public String token { get; init; }
public UInt64 Id { get; init; } public String bearer { get; init; }
public UInt64 idUser { get; init; }
public String verification_mode { get; init; }
public String idAccessToken { get; init; }
public Boolean verification_sent { get; init; }
public Boolean success { get; init; }
} }

24
csharp/App/OpenVpnCertificatesServer/newToken.sh Normal file → Executable file
View File

@ -2,18 +2,30 @@
## USAGE: ./newToken.sh -u USERNAME -p PASSWORD ## USAGE: ./newToken.sh -u USERNAME -p PASSWORD
while getopts u:p: flag while getopts u:p:n: flag
do do
case "${flag}" in case "${flag}" in
u) username=${OPTARG};; u) username=${OPTARG};;
p) password=${OPTARG};; p) password=${OPTARG};;
n) name=${OPTARG};;
esac esac
done done
curl --request POST \ response=$(curl --request POST \
--url https://vrmapi.victronenergy.com/v2/auth/login \ --url https://vrmapi.victronenergy.com/v2/auth/login \
--header 'Content-Type: application/json' \ --header 'Content-Type: application/json' \
--data '{ --data '{"username":"'$username'","password":"'$password'","sms_token":null,"remember_me":false,"language":""}')
"username": '$username',
"password": '$password', echo -n '{"bearer' > token.json
}' > token.json token=$(echo $response | jq ".token")
#echo ${token:1:-1}
uid=$(echo $response | jq ".idUser")
echo -n ${response:7:-1} >> token.json
response2=$(curl --request POST \
--url https://vrmapi.victronenergy.com/v2/users/$uid/accesstokens/create \
--header 'Content-Type: application/json' \
--header 'x-authorization: Bearer '${token:1:-1} \
--data '{"name":"'$name'"}')
echo -n , >> token.json
echo -n ${response2:1} >> token.json

View File

@ -0,0 +1 @@
{"bearer":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjJmYzY5NTNiMjhjNTg3NWQyMzQwNzM3ZjlhNGIzM2RjIn0.eyJ1aWQiOiI1NTQ1MCIsInRva2VuX3R5cGUiOiJkZWZhdWx0IiwiaXNzIjoidnJtYXBpLnZpY3Ryb25lbmVyZ3kuY29tIiwiYXVkIjoiaHR0cHM6Ly92cm1hcGkudmljdHJvbmVuZXJneS5jb20vIiwiaWF0IjoxNjc5NTg2ODU0LCJleHAiOjE2Nzk2NzMyNTQsImp0aSI6IjJmYzY5NTNiMjhjNTg3NWQyMzQwNzM3ZjlhNGIzM2RjIn0.AsLJU7qDPBHO-_FjVo9a8RbyoxhYrDrwZX7V3z4Xq8EoUulv2VyTqy9OXLpez4JI2FVAfSO5a7Amj4XvK1AWtDr9MxP07IBfPyMu5LLGTzjPMAJ6fvZbvZ-eRsP1-aARCFekMGoeSvEEBDrZB9_0kps4h9idQwwGwAby2Tya0vNGu6QEw9WmHcbL8qjvJzxavg8bK6Lttv2-3l_11iZoqiYLdXbwBi32GYK_sdwp-fbGvPE1d6g6eVll94JfSqNLZl9baijtOksx_Qouu7YB8knCgFNrx535d4iJtCkMv9xWztWXbevpSQiy9S8pCgLSpmHNztlVDjacEYyduwUzyw","idUser":55450,"verification_mode":"password","verification_sent":false,"success":true,"token":"108a1407da84afea182b8102a202a2c6b73e9bd6ad919f521033b09837e6e564","idAccessToken":"385119"}