Skip to content

Commit

Permalink
Use customer.bmwgroup.com to get token (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikroe committed Feb 16, 2020
1 parent 914a34e commit ffaf409
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
19 changes: 11 additions & 8 deletions bimmer_connected/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import Callable, List
import requests

from bimmer_connected.country_selector import Regions, get_server_url
from bimmer_connected.country_selector import Regions, get_server_url, get_gcdm_oauth_endpoint
from bimmer_connected.vehicle import ConnectedDriveVehicle
from bimmer_connected.const import AUTH_URL, VEHICLES_URL, ERROR_CODE_MAPPING

Expand Down Expand Up @@ -82,28 +82,31 @@ def _get_oauth_token(self) -> None:

# we really need all of these parameters
values = {
'grant_type': 'password',
'client_id': 'dbf0a542-ebd1-4ff0-a9a7-55172fbfce35',
'response_type': 'token',
'redirect_uri': 'https://www.bmw-connecteddrive.com/app/static/external-dispatch.html',
'scope': 'authenticate_user vehicle_data remote_services',
'username': self._username,
'password': self._password,
}

data = urllib.parse.urlencode(values)
url = AUTH_URL.format(server=self.server_url)
url = AUTH_URL.format(
gcdm_oauth_endpoint=get_gcdm_oauth_endpoint(self._region)
)
try:
response = self.send_request(url, data=data, headers=headers, allow_redirects=False,
expected_response=200, post=True)
expected_response=302, post=True)
except OSError as exception:
msg = 'Authentication failed. Maybe your password is invalid?'
_LOGGER.error(msg)
_LOGGER.exception(exception)
raise OSError(msg) from exception

response_json = response.json()
response_json = dict(urllib.parse.parse_qsl(urllib.parse.urlparse(response.headers['Location']).fragment))

self._oauth_token = response_json['access_token']
# not sure how to use the refresh_token, but might be useful in the future...
self._refresh_token = response_json['refresh_token']
expiration_time = response_json['expires_in']
expiration_time = int(response_json['expires_in'])
self._token_expiration = datetime.datetime.now() + datetime.timedelta(seconds=expiration_time)
_LOGGER.debug('got new token %s with expiration date %s', self._oauth_token, self._token_expiration)

Expand Down
2 changes: 1 addition & 1 deletion bimmer_connected/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""URLs for different services and error code mapping."""

AUTH_URL = 'https://{server}/gcdm/oauth/token'
AUTH_URL = 'https://customer.bmwgroup.com/{gcdm_oauth_endpoint}/authenticate'
BASE_URL = 'https://{server}/webapi/v1'

VEHICLES_URL = BASE_URL + '/user/vehicles'
Expand Down
11 changes: 11 additions & 0 deletions bimmer_connected/country_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class Regions(Enum):
Regions.CHINA: 'b2vapi.bmwgroup.cn:8592'
}

#: Mapping from regions to servers
_GCDM_OAUTH_ENDPOINTS = {
Regions.NORTH_AMERICA: 'gcdm/usa/oauth',
Regions.REST_OF_WORLD: 'gcdm/oauth',
Regions.CHINA: 'gcdm/oauth'
}


def valid_regions() -> List[str]:
"""Get list of valid regions as strings."""
Expand All @@ -42,3 +49,7 @@ def get_region_from_name(name: str) -> Regions:
def get_server_url(region: Regions) -> str:
"""Get the url of the server for the region."""
return _SERVER_URLS[region]

def get_gcdm_oauth_endpoint(region: Regions) -> str:
"""Get the url of the server for the region."""
return _GCDM_OAUTH_ENDPOINTS[region]

0 comments on commit ffaf409

Please sign in to comment.