Skip to content

Commit

Permalink
Update Auth methods (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian authored Jan 10, 2022
1 parent e34075d commit 8f28051
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ Create a new connectedcar `client`
`9fb503e0-715b-47e8-adfd-ad4b7770f73b`

```python
client = connectedcar.AuthClient('9fb503e0-715b-47e8-adfd-ad4b7770f73b', None, None)
client = connectedcar.AuthClient('9fb503e0-715b-47e8-adfd-ad4b7770f73b', None, None, None, 'US') # Region argument is only required if you live outside the United States.
```

- Note: If your region is outside of the US you can pass different region parameters to the User class. Regions: (US, CA, EU, AU)

Use `client.get_user_access_token()` to exchange your user credentials for an **access object**. To make any vehicle data request to the Ford Sync Connect API, you'll need to give the SDK a valid **access token**.

```python
Expand Down Expand Up @@ -62,8 +64,6 @@ With your access token in hand, use `connectedcar.User()` to get a User object r
user = connectedcar.User(access['access_token'], "US") # Region argument is only required if you live outside the United States.
```

- Note: If your region is outside of the US you can pass different region parameters to the User class. Regions: (US, CA, EU, AU)

Use `user.vehicles()` to return an array of all the vehicles associated with a users account. The response will include the **vehicle vin**.

```python
Expand All @@ -81,8 +81,6 @@ Now with a **vehicle vin** in hand, use `connectedcar.Vehicle()` to get a Vehicl
currentVehicle = connectedcar.Vehicle(vehicleList[0], access['access_token'], "US") # Region argument is only required if you live outside the United States.
```

- Note: If your region is outside of the US you can pass different region parameters to the Vehicle class. Regions: (US, CA, EU, AU)

Now you can ask the car to do things, or ask it for some data! For example:

```python
Expand Down
42 changes: 34 additions & 8 deletions connectedcar/connectedcar.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import json
from . import const, requester
import requests


class AuthClient(object):

regions = {
'US': '71A3AD0A-CF46-4CCF-B473-FC7FE5BC4592',
'CA': '71A3AD0A-CF46-4CCF-B473-FC7FE5BC4592',
'EU': '1E8C7794-FF5F-49BC-9596-A1E0C86C5B19',
'AU': '5C80A6BB-CF0D-4A30-BDBF-FC804B5C1A98',
}

def __init__(self, client_id, client_secret,
redirect_uri=None, scope=None):
redirect_uri=None, scope=None, region='US'):
""" A client for accessing the Ford API
Args:
Expand All @@ -20,6 +29,7 @@ def __init__(self, client_id, client_secret,
self.client_id = client_id
self.client_secret = client_secret
self.redirect_uri = redirect_uri
self.region = self.regions[region]

def get_user_access_token(self, username, password):
""" Exchange a username and password for a new access dictionary
Expand All @@ -40,7 +50,7 @@ def get_user_access_token(self, username, password):
'Accept-Language': 'en-US',
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'fordpass-na/353 CFNetwork/1121.2.2 Darwin/19.3.0',
'Accept-Encoding': 'gzip, deflate, br'
'Accept-Encoding': 'gzip, deflate, br',
}

data = {
Expand All @@ -52,7 +62,24 @@ def get_user_access_token(self, username, password):

response = requester.call(
'POST', const.TOKEN_URL, headers=headers, data=data).json()
return response

if (response['access_token']):

headers['Content-Type'] = 'application/json'
headers['Application-Id'] = self.region

data = {
'code': response['access_token']
}

response = requester.call(
'PUT', 'https://api.mps.ford.com/api/oauth2/v1/token', headers=headers, data=json.dumps(data)).json()

return response

else:
raise Exception("Access Token was not returned")


def exchange_refresh_token(self, refresh_token):
""" Exchange a refresh token for a new access dictionary
Expand All @@ -71,17 +98,16 @@ def exchange_refresh_token(self, refresh_token):
headers = {
'Accept': '*/*',
'Accept-Language': 'en-US',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Type': 'application/json',
'User-Agent': 'fordpass-na/353 CFNetwork/1121.2.2 Darwin/19.3.0',
'Accept-Encoding': 'gzip, deflate, br'
'Accept-Encoding': 'gzip, deflate, br',
'Application-Id': self.region
}

data = {
'client_id': self.client_id,
'grant_type': 'refresh_token',
'refresh_token': refresh_token
}

response = requester.call(
'POST', const.TOKEN_URL, headers=headers, data=data).json()
'PUT', 'https://api.mps.ford.com/api/oauth2/v1/refresh', headers=headers, data=json.dumps(data)).json()
return response
2 changes: 2 additions & 0 deletions connectedcar/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def call(method, url, **kwargs):
response = requests.request(method, url, timeout=310, **kwargs)
code = response.status_code

print(response.json())

if response.ok:
return response
elif code == 400:
Expand Down

0 comments on commit 8f28051

Please sign in to comment.