bimmerconnected / bimmer_connected

🚘 Library to query the status of your BMW or Mini from the ConnectedDrive portal
Apache License 2.0
362 stars 79 forks source link

Failed to login with the error 'Forbidden for url: https://myprofile.bmw.com.cn/eadrax-coas/v1/cop/publickey' #408

Closed muxiachuixue closed 2 years ago

muxiachuixue commented 2 years ago

Describe the issue

The integration in HA suddenly gave me an error this morning: Forbidden for url: https://myprofile.bmw.com.cn/eadrax-coas/v1/cop/publickey I've tried to reboot the host and delete the integration then re-add it. However, no luck. I opened the url https://myprofile.bmw.com.cn/eadrax-coas/v1/cop/publickey in Chrome on my laptop (different network from the HA) and got the error 403 Forbidden. Microsoft-Azure-Application-Gateway/v2. Strangely, when I used Safari or IE to open it, the response is OK. I am not running any proxy server on HA and other integrations run perfectly. I guess there may be something wrong with the parameters of HTTP request.

Edit: I used Charles to capture the packages of HTTP request of the url https://myprofile.bmw.com.cn/eadrax-coas/v1/cop/publickey in Chrome. I found that If I delete the Cookie parameter in the header, the response will be OK. In default, the Cookie parameter is added and a 403 Forbidden error is returned.

Expected behavior

Fix the Forbidden for url: https://myprofile.bmw.com.cn/eadrax-coas/v1/cop/publickey error.

Which Home Assistant version are you using?

core-2021.12.9

What was the last working version of Home Assistant Core?

core-2021.12.8

What is your region?

China

ConnectedDrive website

Number of cars

Output of bimmer_connected fingerprint

pi@raspberrypi:~ $ ha info
arch: armv7
channel: stable
docker: 20.10.8
features:
- reboot
- shutdown
- services
- network
- hostname
- timedate
hassos: null
homeassistant: 2021.12.9
hostname: raspberrypi
logging: info
machine: raspberrypi4
operating_system: Raspbian GNU/Linux 10 (buster)
state: running
supervisor: 2021.12.2
supported: false
supported_arch:
- armv7
- armhf
timezone: Asia/Shanghai
Logger: bimmer_connected.account
Source: /usr/local/lib/python3.9/site-packages/bimmer_connected/account.py:263
First occurred: 09:15:44 (1 occurrences)
Last logged: 09:15:44

Authentication failed: <html> <head><title>403 Forbidden</title></head> <body> <center><h1>403 Forbidden</h1></center> <hr><center>Microsoft-Azure-Application-Gateway/v2</center> </body> </html>
Logger: bimmer_connected.account
Source: /usr/local/lib/python3.9/site-packages/bimmer_connected/account.py:107
First occurred: 09:15:44 (2 occurrences)
Last logged: 09:22:45

403 Client Error: Forbidden for url: https://myprofile.bmw.com.cn/eadrax-coas/v1/cop/publickey
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/bimmer_connected/account.py", line 96, in _get_oauth_token
    token_data = self._login_china()
  File "/usr/local/lib/python3.9/site-packages/bimmer_connected/account.py", line 264, in _login_china
    raise ex
  File "/usr/local/lib/python3.9/site-packages/bimmer_connected/account.py", line 231, in _login_china
    response.raise_for_status()
  File "/usr/local/lib/python3.9/site-packages/requests/models.py", line 953, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://myprofile.bmw.com.cn/eadrax-coas/v1/cop/publickey
pi@raspberrypi:/usr/share/hassio/homeassistant $ bimmerconnected status 86152xxxxxxxx mypassword china
DEBUG:bimmer_connected.account:Getting vehicle list
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): myprofile.bmw.com.cn:443
DEBUG:urllib3.connectionpool:https://myprofile.bmw.com.cn:443 "GET /eadrax-coas/v1/cop/publickey HTTP/1.1" 403 179
ERROR:bimmer_connected.account:Authentication failed: <html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>Microsoft-Azure-Application-Gateway/v2</center>
</body>
</html>

ERROR:bimmer_connected.account:403 Client Error: Forbidden for url: https://myprofile.bmw.com.cn/eadrax-coas/v1/cop/publickey
Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/account.py", line 96, in _get_oauth_token
    token_data = self._login_china()
  File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/account.py", line 264, in _login_china
    raise ex
  File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/account.py", line 231, in _login_china
    response.raise_for_status()
  File "/home/pi/.local/lib/python3.7/site-packages/requests/models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://myprofile.bmw.com.cn/eadrax-coas/v1/cop/publickey
Traceback (most recent call last):
  File "/home/pi/.local/bin/bimmerconnected", line 10, in <module>
    sys.exit(main())
  File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/cli.py", line 282, in main
    args.func(args)
  File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/cli.py", line 101, in get_status
    account = ConnectedDriveAccount(args.username, args.password, get_region_from_name(args.region))
  File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/account.py", line 83, in __init__
    self._get_vehicles()
  File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/account.py", line 376, in _get_vehicles
    self._get_oauth_token()
  File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/account.py", line 108, in _get_oauth_token
    raise ex
  File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/account.py", line 96, in _get_oauth_token
    token_data = self._login_china()
  File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/account.py", line 264, in _login_china
    raise ex
  File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/account.py", line 231, in _login_china
    response.raise_for_status()
  File "/home/pi/.local/lib/python3.7/site-packages/requests/models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://myprofile.bmw.com.cn/eadrax-coas/v1/cop/publickey
pi@raspberrypi:/usr/share/hassio/homeassistant $ curl https://myprofile.bmw.com.cn/eadrax-coas/v1/cop/publickey
{"data":{"value":"-----BEGIN PUBLIC KEY-----\r\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCteEZFIGa2z5cj7sAmX40y8/ige01T2r+VUzkMshAYwotZFvrVWZLQ6W9+ltvINJoRfZEZkmdP2lsidhqj1H1+RWyC78ear7Fm6xd9Gp9LnKtVVBJRM/9cBRg0AGiTJ7IO/x6MpKkBxxHmProFqPI40hueunV85RlaPBrjZVNIpQIDAQAB\r\n-----END PUBLIC KEY-----","expires":"3600"},"code":200,"error":false,"description":"ok"}
rikroe commented 2 years ago

Thanks for the detailed report! Indeed it seems that BMW have opted to shut down their API by filtering out user agents...

muxiachuixue commented 2 years ago

Thanks for the detailed report! Indeed it seems that BMW have opted to shut down their API by filtering out user agents...

Thanks. I modified the codes in account.py and now the integration in HA works well.

github-actions[bot] commented 2 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.