tdorssers / TeslaPy

A Python module to use the Tesla Motors Owner API
MIT License
374 stars 83 forks source link

Calling api() returns 404 error #112

Closed hafley closed 1 year ago

hafley commented 2 years ago

invite.py

import teslapy
import webview

email = input("Email address: ")

def custom_auth(url):
    result = [""]
    window = webview.create_window("Login", url)    
    def on_loaded():
        result[0] = window.get_current_url()
        if "void/callback" in result[0].split("?")[0]:
            window.destroy()
    try:
        window.events.loaded += on_loaded
    except AttributeError:
        window.loaded += on_loaded      
    webview.start()
    return result[0]    

with teslapy.Tesla(email, authenticator=custom_auth) as tesla:
    tesla.fetch_token()

with teslapy.Tesla(email) as tesla:
    vehicles = tesla.vehicle_list()
    vehicle_id = vehicles[0]["vehicle_id"]
    vehicle_dict = {"vehicle_id": vehicle_id}
    endpoint ="FETCH_VEHICLE_SHARE_INVITES"
    tesla.api(endpoint,vehicle_dict)

Terminal output:

Traceback (most recent call last):

File "invite.py", line 25, in tesla.api(endpoint,vehicle_dict)

File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/teslapy/init.py", line 363, in api return self.request(endpoint['TYPE'], uri, serialize, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/teslapy/init.py", line 155, in request response.raise_for_status() # Raise HTTPError, if one occurred ^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/requests/models.py", line 1021, in raise_for_status raise HTTPError(http_error_msg, response=self)

requests.exceptions.HTTPError: 404 Client Error: not_found for url: https://owner-api.teslamotors.com/api/1/vehicles/265113217424/invitations

tdorssers commented 1 year ago

Please change last context manager to:

with teslapy.Tesla(email) as tesla:
    vehicles = tesla.vehicle_list()
    print(vehicles[0].api("FETCH_VEHICLE_SHARE_INVITES"))

If you call api() on the vehicle object, the vehicle_id is added automatically.