@@ -265,14 +265,6 @@ var (
265265 domainName []byte
266266)
267267
268- func getBaseURL (c * fiber.Ctx ) string {
269- protocol := "http"
270- if c .Protocol () == "https" || c .Get ("X-Forwarded-Proto" ) == "https" {
271- protocol = "https"
272- }
273- return fmt .Sprintf ("%s://%s" , protocol , c .Hostname ())
274- }
275-
276268func getRedirectURI (c * fiber.Ctx ) string {
277269 host := c .Hostname ()
278270 // Use exact URIs as registered in Tesla Developer Portal
@@ -349,12 +341,16 @@ func exchangeAuthCode(code string, c *fiber.Ctx) (*TokenResponse, error) {
349341}
350342
351343func getVehicles (token string ) (* VehicleResponse , error ) {
352- req , err := http .NewRequest ("GET" , "https://fleet-api.prd.na.vn.cloud.tesla.com/api/1/vehicles" , nil )
344+ url := "https://fleet-api.prd.na.vn.cloud.tesla.com/api/1/vehicles"
345+ log .Printf ("Making request to Fleet API: GET %s" , url )
346+
347+ req , err := http .NewRequest ("GET" , url , nil )
353348 if err != nil {
354349 return nil , fmt .Errorf ("error creating request: %v" , err )
355350 }
356351
357352 req .Header .Set ("Authorization" , "Bearer " + token )
353+ log .Printf ("Request headers: %v" , req .Header )
358354
359355 client := & http.Client {}
360356 resp , err := client .Do (req )
@@ -368,8 +364,8 @@ func getVehicles(token string) (*VehicleResponse, error) {
368364 return nil , fmt .Errorf ("error reading response: %v" , err )
369365 }
370366
371- log .Printf ("Vehicles response status: %d" , resp . StatusCode )
372- log . Printf ( "Vehicles response body: %s" , string (body ))
367+ log .Printf ("Fleet API Response for GET %s: \n Status: %d\n Headers: %v \n Body: %s" ,
368+ url , resp . StatusCode , resp . Header , string (body ))
373369
374370 if resp .StatusCode != http .StatusOK {
375371 return nil , fmt .Errorf ("failed to get vehicles with status %d: %s" , resp .StatusCode , string (body ))
@@ -383,15 +379,6 @@ func getVehicles(token string) (*VehicleResponse, error) {
383379 return & vehicleResp , nil
384380}
385381
386- func getVirtualKeyURL () string {
387- domain := strings .TrimSpace (string (domainName ))
388- domain = strings .ToLower (domain )
389- domain = strings .TrimPrefix (domain , "https://" )
390- domain = strings .TrimPrefix (domain , "http://" )
391- domain = strings .TrimSuffix (domain , "/" )
392- return fmt .Sprintf ("https://www.tesla.com/_ak/%s" , domain )
393- }
394-
395382// Update configure telemetry function
396383func configureTelemetry (vin , accessToken string ) error {
397384 // Tesla's proxy runs on localhost:4443 with HTTPS
@@ -465,10 +452,9 @@ func configureTelemetry(vin, accessToken string) error {
465452 req .Header .Set ("Content-Type" , "application/json" )
466453 req .Header .Set ("Authorization" , "Bearer " + accessToken )
467454
468- // Create a custom HTTP client that skips TLS verification for localhost
469455 tr := & http.Transport {
470456 TLSClientConfig : & tls.Config {
471- InsecureSkipVerify : true , // Skip verification since it's localhost
457+ InsecureSkipVerify : true ,
472458 },
473459 }
474460 client := & http.Client {Transport : tr }
@@ -480,13 +466,33 @@ func configureTelemetry(vin, accessToken string) error {
480466 defer resp .Body .Close ()
481467
482468 body , _ := io .ReadAll (resp .Body )
469+ log .Printf ("Telemetry configuration response: %s" , string (body ))
470+
471+ // Parse the response to check for missing key
472+ var result struct {
473+ Response struct {
474+ UpdatedVehicles int `json:"updated_vehicles"`
475+ SkippedVehicles struct {
476+ MissingKey []string `json:"missing_key"`
477+ } `json:"skipped_vehicles"`
478+ } `json:"response"`
479+ }
480+
481+ if err := json .Unmarshal (body , & result ); err != nil {
482+ return fmt .Errorf ("error parsing response: %v" , err )
483+ }
484+
485+ // Check if this VIN is in the missing_key list
486+ for _ , missingVIN := range result .Response .SkippedVehicles .MissingKey {
487+ if missingVIN == vin {
488+ return fmt .Errorf ("missing_key: Virtual key not paired with vehicle. Please pair your virtual key at https://www.tesla.com/_ak/tesla.rajsingh.info" )
489+ }
490+ }
491+
483492 if resp .StatusCode != http .StatusOK {
484493 return fmt .Errorf ("failed to configure telemetry with status %d: %s" , resp .StatusCode , string (body ))
485494 }
486495
487- // Log the response for debugging
488- log .Printf ("Telemetry configuration response: %s" , string (body ))
489-
490496 return nil
491497}
492498
@@ -526,12 +532,16 @@ func getUserInfo(accessToken string) (*UserInfo, error) {
526532}
527533
528534func getTeslaUserInfo (accessToken string ) (* TeslaUserResponse , error ) {
529- req , err := http .NewRequest ("GET" , "https://fleet-api.prd.na.vn.cloud.tesla.com/api/1/users/me" , nil )
535+ url := "https://fleet-api.prd.na.vn.cloud.tesla.com/api/1/users/me"
536+ log .Printf ("Making request to Fleet API: GET %s" , url )
537+
538+ req , err := http .NewRequest ("GET" , url , nil )
530539 if err != nil {
531540 return nil , fmt .Errorf ("error creating request: %v" , err )
532541 }
533542
534543 req .Header .Set ("Authorization" , "Bearer " + accessToken )
544+ log .Printf ("Request headers: %v" , req .Header )
535545
536546 client := & http.Client {}
537547 resp , err := client .Do (req )
@@ -545,8 +555,8 @@ func getTeslaUserInfo(accessToken string) (*TeslaUserResponse, error) {
545555 return nil , fmt .Errorf ("error reading response: %v" , err )
546556 }
547557
548- log .Printf ("Tesla user info response status: %d" , resp . StatusCode )
549- log . Printf ( "Tesla user info response body: %s" , string (body ))
558+ log .Printf ("Fleet API Response for GET %s: \n Status: %d\n Headers: %v \n Body: %s" ,
559+ url , resp . StatusCode , resp . Header , string (body ))
550560
551561 if resp .StatusCode != http .StatusOK {
552562 return nil , fmt .Errorf ("failed to get Tesla user info with status %d: %s" , resp .StatusCode , string (body ))
0 commit comments