stefan.kern
For Tesla Drivers here December 01, 2019 11:10PM |
//////////////////////////////////////////////////////////////////////////////////// //1St Step: Get an access Token using the credentials from the original Tesla App //////////////////////////////////////////////////////////////////////////////////// //VARIABLES USED: vLogin is Variant //Variant holding the parameters to pass to tesla server to get a token: RequestLogin is httpRequest // HTTP request var against the tesla serber LoginResponse is httpResponse //Response from tesla server sAcessToken is string //After authentification we need this token. It is valid for 45days sRefreshToken is string //We can use this token, to get an new token after 45 days ..... vLoginResponse is Variant //Variant holding the content of loginrespoinse vLogin.grant_type = "password" vLogin.client_id = "MyClientID" vLogin.client_secret = "MySecretCode" vLogin.email = "MyEmailAddress" vLogin.password = "MyPassword" //Now we are doing a HTTP POST to Tesla Server to get the token RequestLogin.URL = "[owner-api.teslamotors.com]; RequestLogin.Content = VariantToJSON(vLogin) RequestLogin..ContentType = typeMimeJSON LoginResponse = HTTPSend(RequestLogin) vLoginResponse = JSONToVariant(LoginResponse.Content) //VERY BASIC ERROR HANDLING IF ErrorOccurred THEN Error(ErrorInfo(errFullDetails)) END IF LoginResponse.StatusCode <> 200 THEN Info("Fehler, invalid credentials") RETURN END sAcessToken = "Bearer " + vLoginResponse.access_token sRefreshToken = "Bearer " + vLoginResponse.refres_token /////////////////////////////////////////////////////////////////////////////// //AFTER LOGIN WE CAN ASK THE TESLA SERVER FOR ALL CARS LINKED TO THE ACCOUNT. //We need to get the car id we want to work with. I have only one tesla, so //this code can handle only one. ////////////////////////////////////////////////////////////////////////////// //Variables uses: CarListRequest is httpRequest() CarListRespose is httpResponse vCarListResponse is Variant sVehicleID is string //The vehicle we want to handle //Ask Tesla Server for car list and put it into the vCarListReponse Variables CarListRequest.URL = "[owner-api.teslamotors.com]; CarListRequest..ContentType = typeMimeJSON CarListRequest.Header["Authorization"] = sAcessToken CarListRespose = HTTPSend(CarListRequest) vCarListResponse = JSONToVariant(CarListRespose.Content) sVehicleID = vCarListResponse.response[1].id_s //Lucky people owning more than one tesla need to code a bit more //=> vCarLIstResponse shows data like: Name of the car, options, colour, total milage and much more /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Now we can to some things, like reading the car state: ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Variables uses: requestcarstate is httpRequest ResponseCarState is httpResponse vResponseCarState is Variant //Get infos like: requestcarstate.URL = "[owner-api.teslamotors.com]; requestcarstate.Header["Authorization"] = sAcessToken ResponseCarState = HTTPSend(requestcarstate) vResponseCarState =JSONToVariant(ResponseCarState.Content) //Shows a lot of car information /////////////////////////////////////////////////////////////////////////////// // lets hunk the horn // you can also start climate control, open or lock the doors and a lot more /////////////////////////////////////////////////////////////////////////////// //Var used: RequestHonkHorn is httpRequest ResponseHonkHorn is httpResponse vResponseHonkHorn is Variant //HONK the horn ////[owner-api.teslamotors.com] RequestHonkHorn.URL = "[owner-api.teslamotors.com]; RequestHonkHorn.Header["Authorization"] = sAcessToken RequestHonkHorn.Method=httpPost ResponseHonkHorn = HTTPSend(RequestHonkHorn) vResponseHonkHorn = JSONToVariant(ResponseHonkHorn.Content) IF vResponseHonkHorn.response.result = True THEN Info("Honk") ELSE Info("Error") END
stefan.kern
Re: For Tesla Drivers here December 01, 2019 11:15PM |
Re: For Tesla Drivers here December 04, 2019 08:21AM |
Registered: 5 years ago Posts: 163 |
stefan.kern
Re: For Tesla Drivers here December 04, 2019 03:50PM |
Re: For Tesla Drivers here December 05, 2019 06:06PM |
Registered: 5 years ago Posts: 163 |