Skip to content

Instantly share code, notes, and snippets.

@charleyhine
Last active September 22, 2018 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charleyhine/2d42e1b2f9a62e80c95fb8998ebeab28 to your computer and use it in GitHub Desktop.
Save charleyhine/2d42e1b2f9a62e80c95fb8998ebeab28 to your computer and use it in GitHub Desktop.
Tesla API
require 'httparty'
require 'pp'
class Tesla
TESLA_CLIENT_ID = '81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384'
TESLA_CLIENT_SECRET = 'c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3'
include HTTParty
base_uri 'https://owner-api.teslamotors.com'
def initialize(email, password)
options = {
query: {
grant_type: 'password',
client_id: TESLA_CLIENT_ID,
client_secret: TESLA_CLIENT_SECRET,
email: email,
password: password
}
}
resp = self.class.post('/oauth/token', options)
access_token = resp['access_token']
@headers = {
headers: {
authorization: "Bearer #{access_token}",
'User-Agent' => 'Mozilla/5.0 (Linux; Android 9.0.0; VS985 4G Build/LRX21Y; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/58.0.3029.83 Mobile Safari/537.36',
'X-Tesla-User-Agent' => 'TeslaApp/3.4.4-350/fad4a582e/android/9.0.0'
}
}
end
def products
resp = self.class.get("/api/1/products", @headers)
pp resp['response']
end
def vehicles
resp = self.class.get('/api/1/vehicles', @headers)
pp resp['response']
end
def vehicle_all_data(vehicle_id=first_vehicle_id)
resp = self.class.get("/api/1/vehicles/#{vehicle_id}/data", @headers)
pp resp['response']
end
def vehicle_state(vehicle_id=first_vehicle_id)
resp = self.class.get("/api/1/vehicles/#{vehicle_id}/data_request/vehicle_state", @headers)
pp resp['response']
end
def vehicle_gui_settings(vehicle_id=first_vehicle_id)
resp = self.class.get("/api/1/vehicles/#{vehicle_id}/data_request/gui_settings", @headers)
pp resp['response']
end
def vehicle_drive_state(vehicle_id=first_vehicle_id)
resp = self.class.get("/api/1/vehicles/#{vehicle_id}/data_request/drive_state", @headers)
pp resp['response']
end
private
def first_vehicle_id
@first_vehicle_id ||= vehicles.first['id']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment