mandarons / icloudpy

iCloud web service interface library in Python
Other
195 stars 20 forks source link

[BUG] ICloudPyAPIResponseException from China account. #22

Closed lichengwu closed 1 year ago

lichengwu commented 1 year ago

When do authentication using:

sudo docker exec -it iCloud /bin/sh -c "icloud --username=ol_l@msn.cn --region=china --session-directory=/app/session_data"
Enter iCloud password for ol_l@msn.cn:
Save password in keyring? [y/N]: y

Two-step authentication required.
Please enter validation code
(string) -->

The docker container crashed with the following errors:

Loading config from /app/config.yaml ...
2023-03-18 16:07:15,413 :: ERROR :: icloudpy.base :: base.py :: 186 :: Authentication required for Account. (421)
2023-03-18 16:07:15,735 :: ERROR :: icloudpy.base :: base.py :: 186 :: Not Found (404)
Traceback (most recent call last):
  File "/app/venv/lib/python3.10/site-packages/icloudpy/base.py", line 336, in authenticate
    self.session.post(
  File "/app/venv/lib/python3.10/site-packages/requests/sessions.py", line 635, in post
    return self.request("POST", url, data=data, json=json, **kwargs)
  File "/app/venv/lib/python3.10/site-packages/icloudpy/base.py", line 131, in request
    self._raise_error(response.status_code, response.reason)
  File "/app/venv/lib/python3.10/site-packages/icloudpy/base.py", line 187, in _raise_error
    raise api_error
icloudpy.exceptions.ICloudPyAPIResponseException: Not Found (404)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/app/./src/main.py", line 7, in <module>
    sync.sync()
  File "/app/src/sync.py", line 68, in sync
    api = get_api_instance(
  File "/app/src/sync.py", line 30, in get_api_instance
    ICloudPyService(
  File "/app/venv/lib/python3.10/site-packages/icloudpy/base.py", line 277, in __init__
    self.authenticate()
  File "/app/venv/lib/python3.10/site-packages/icloudpy/base.py", line 344, in authenticate
    raise ICloudPyFailedLoginException(msg, error) from error
icloudpy.exceptions.ICloudPyFailedLoginException: ('Invalid email/password combination.', ICloudPyAPIResponseException('Not Found (404)'))

My account/password is correct, and i received the 2FA. But it crashed when i want to enter validation code.

mandarons commented 1 year ago

Chinese servers have been hit and miss. Can you set logging to debug in the config file and run it again? Please share relevant logs.

lichengwu commented 1 year ago

2023-03-19 01:01:01,741 :: WARNING :: root :: notify.py :: 47 :: Not sending 2FA notification because SMTP is not configured Loading config from /app/config.yaml ... 2023-03-19 01:01:02,758 :: DEBUG :: root :: config_parser.py :: 101 :: Checking root destination ... 2023-03-19 01:01:02,760 :: DEBUG :: urllib3.connectionpool :: connectionpool.py :: 1003 :: Starting new HTTPS connection (1): wapar-api.mandarons.com:443 2023-03-19 01:01:04,501 :: DEBUG :: urllib3.connectionpool :: connectionpool.py :: 456 :: https://wapar-api.mandarons.com:443 "POST /api/heartbeat HTTP/1.1" 201 45 2023-03-19 01:01:04,506 :: DEBUG :: icloudpy.base :: base.py :: 246 :: Using session file session_data/ol_lmsncn.session 2023-03-19 01:01:04,512 :: DEBUG :: icloudpy.base :: base.py :: 270 :: Read cookies from session_data/ol_lmsncn 2023-03-19 01:01:04,512 :: DEBUG :: icloudpy.base :: base.py :: 291 :: Checking session token validity 2023-03-19 01:01:04,513 :: DEBUG :: icloudpy.base :: base.py :: 390 :: Checking session token validity 2023-03-19 01:01:04,579 :: DEBUG :: icloudpy.base.http :: base.py :: 75 :: POST https://setup.icloud.com.cn/setup/ws/1/validate null 2023-03-19 01:01:04,583 :: DEBUG :: urllib3.connectionpool :: connectionpool.py :: 1003 :: Starting new HTTPS connection (1): setup.icloud.com.cn:443 2023-03-19 01:01:04,853 :: DEBUG :: urllib3.connectionpool :: connectionpool.py :: 456 :: https://setup.icloud.com.cn:443 "POST /setup/ws/1/validate HTTP/1.1" 421 442 2023-03-19 01:01:04,856 :: DEBUG :: icloudpy.base :: base.py :: 94 :: Saved session data to file 2023-03-19 01:01:04,893 :: DEBUG :: icloudpy.base :: base.py :: 98 :: Cookies saved to session_data/ol_lmsncn 2023-03-19 01:01:04,893 :: DEBUG :: icloudpy.base.http :: base.py :: 127 :: Misdirected Request (421). Retrying ... 2023-03-19 01:01:04,895 :: DEBUG :: requests.sessions.http :: base.py :: 75 :: POST https://setup.icloud.com.cn/setup/ws/1/validate null 2023-03-19 01:01:05,113 :: DEBUG :: urllib3.connectionpool :: connectionpool.py :: 456 :: https://setup.icloud.com.cn:443 "POST /setup/ws/1/validate HTTP/1.1" 421 442 2023-03-19 01:01:05,116 :: DEBUG :: icloudpy.base :: base.py :: 94 :: Saved session data to file 2023-03-19 01:01:05,119 :: DEBUG :: icloudpy.base :: base.py :: 98 :: Cookies saved to session_data/ol_lmsncn 2023-03-19 01:01:05,120 :: ERROR :: icloudpy.base :: base.py :: 186 :: Authentication required for Account. (421) 2023-03-19 01:01:05,120 :: DEBUG :: icloudpy.base :: base.py :: 396 :: Invalid authentication token 2023-03-19 01:01:05,121 :: DEBUG :: icloudpy.base :: base.py :: 296 :: Invalid authentication token, will log in from scratch. 2023-03-19 01:01:05,121 :: DEBUG :: icloudpy.base :: base.py :: 318 :: Authenticating as ol_l@msn.cn 2023-03-19 01:01:05,123 :: DEBUG :: icloudpy.base.http :: base.py :: 75 :: POST https://www.icloud.com.cn/signin {"accountName": "ol_l@msn.cn", "password": "****", "rememberMe": true, "trustTokens": []} 2023-03-19 01:01:05,127 :: DEBUG :: urllib3.connectionpool :: connectionpool.py :: 1003 :: Starting new HTTPS connection (1): www.icloud.com.cn:443 2023-03-19 01:01:05,475 :: DEBUG :: urllib3.connectionpool :: connectionpool.py :: 456 :: https://www.icloud.com.cn:443 "POST /signin?isRememberMeEnabled=true HTTP/1.1" 404 1511 2023-03-19 01:01:05,477 :: DEBUG :: icloudpy.base :: base.py :: 94 :: Saved session data to file 2023-03-19 01:01:05,515 :: DEBUG :: icloudpy.base :: base.py :: 98 :: Cookies saved to session_data/ol_lmsncn 2023-03-19 01:01:05,515 :: ERROR :: icloudpy.base :: base.py :: 186 :: Not Found (404) Traceback (most recent call last): File "/app/venv/lib/python3.10/site-packages/icloudpy/base.py", line 336, in authenticate self.session.post( File "/app/venv/lib/python3.10/site-packages/requests/sessions.py", line 635, in post return self.request("POST", url, data=data, json=json, **kwargs) File "/app/venv/lib/python3.10/site-packages/icloudpy/base.py", line 131, in request self._raise_error(response.status_code, response.reason) File "/app/venv/lib/python3.10/site-packages/icloudpy/base.py", line 187, in _raise_error raise api_error icloudpy.exceptions.ICloudPyAPIResponseException: Not Found (404)

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/app/./src/main.py", line 7, in sync.sync() File "/app/src/sync.py", line 68, in sync api = get_api_instance( File "/app/src/sync.py", line 30, in get_api_instance ICloudPyService( File "/app/venv/lib/python3.10/site-packages/icloudpy/base.py", line 277, in init self.authenticate() File "/app/venv/lib/python3.10/site-packages/icloudpy/base.py", line 344, in authenticate raise ICloudPyFailedLoginException(msg, error) from error icloudpy.exceptions.ICloudPyFailedLoginException: ('Invalid email/password combination.', ICloudPyAPIResponseException('Not Found (404)'))

mandarons commented 1 year ago

Looks like POST https://www.icloud.com.cn/signin {"accountName": "ol_l@msn.cn", "password": "********", "rememberMe": true, "trustTokens": []} is failing. Not sure why. I don't have iCloud.com.cn account and hence, cannot reproduce it on my end. :(

Not sure how to help you here.

zhouyi619 commented 1 year ago

Based on the web requests I observed at icloud.com.cn, I It appear to be that apple changed auth_endpoint back to "idmsa.apple.com". But when I remove "auth_endpoint" param,it failed again and the logs said it got a 302 when requesting "https://setup.icloud.com.cn/setup/ws/1/accountLogin".I tried to resend this request using apifox but got the correct response.The only things I set up in apifox was "Origin" and "Referer" Headers and Body copied from logs. Here's my log if that helps

DEBUG:icloudpy.base:Using session file C:\Users\zhouy\AppData\Local\Temp\icloudpy\zhouy\zy2658970293gmailcom.session
INFO:icloudpy.base:Session file does not exist
DEBUG:icloudpy.base:Authenticating as zy2658970293@gmail.com
DEBUG:icloudpy.base.http:POST https://idmsa.apple.com/appleauth/auth/signin {"accountName": "zy2658970293@gmail.com", "password": "********", "rememberMe": true, "trustTokens": []}
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): idmsa.apple.com:443
DEBUG:urllib3.connectionpool:https://idmsa.apple.com:443 "POST /appleauth/auth/signin?isRememberMeEnabled=true HTTP/1.1" 200 None
DEBUG:icloudpy.base:Saved session data to file
DEBUG:icloudpy.base:Cookies saved to C:\Users\zhouy\AppData\Local\Temp\icloudpy\zhouy\zy2658970293gmailcom
DEBUG:icloudpy.base.http:POST https://setup.icloud.com.cn/setup/ws/1/accountLogin {"accountCountryCode": "CHN", "dsWebAuthToken": "4f6/EzTBOP6iyFOXBMNESoap0TldLk/zhGzRTMgASgbGF0AWKb4W5Eac2vBpJnTs3peuGBPwxfDM8WF+eSDVt38sa3A+2YhO4r/+IvooZFvy3pjz42h5hx1bGGgXIFsklj6aouTXmx9AqNuojf2G2WYp+qKDejLARn6WsLoOFSP6V0E+cAcntDZk5pIQu/mjpYxXL0up/pzTIbPwttUF+GXExf6fIiFYahQ5+VSrJZJ4v/5/h6joR9jtUMNRbKUJauj788fbPK4ekYwx31uKlXpmlfwS+sK0fgiPr0C24qbXCEyNJ2cg1IjMpenjpN9EcJ7GaI5i+G3RwmLbAJndp1/EbU/HZ0306sHS6SQTmKzCHaA7e06LCK57cFL9DySeGxkKLNV80pn4NDphwe1TQGPyA0rIdnM86e8Ba7ec4BqwHV+uIMZNOBSNfg0Yod68PngSbwCuijb3IU3o2DMWIH/o3XtyO+nEZcFBkDrfc6oMG78lF/U3U40CJiC/sq0LeftYXgOHAKhBevQES6gld9OCuEpfcNC9r7HIqJVU1z0r0truqtmqFsLTq4mSTpuCDs8N6gMPtqsxAORzClpWFJfIaXXcWFK2NygTexIQod/5MU/0efVAKkTaj+9Qjqt4A2T9AxGI56T31N1TrgPmhipCKvGQxtf9/9Xo0hyST7FvhAUsgBp/T5GUCgOmx7ZnGIAJCdi+FYv+z+ykzamuMk7zB5sbzcetEphRggtPd6KIcC+i50QoIUCF0V0MwUDQ8ztsVvPp+kc4LEtIBeM9R4soRdcbrcBffZ2C7Xum5mNbY+W+aqBaGzQ7OFwxPIVchmOf0B2x89zG7x+0AOjTOS3p0R+p3aChmHqU/WtsDChU/m0CDzBda++emf41cCW4mt0OapRuzVcz780yFH4KmgiIN2zFyArrw1+ogcYaWQv00PjLA5YfzTK/NrPSAxLmmxfJZW43Kd6beuzTHWnHo1fjD7KhiA06WzHvrqIWU7b6RVYajCpSqnDKEoL9hxE7wWxbaTcNhskigv8nj0Pppvkkcq6+AA19X4F3QYM=", "extended_login": true, "trustToken": ""}
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): setup.icloud.com.cn:443
DEBUG:urllib3.connectionpool:https://setup.icloud.com.cn:443 "POST /setup/ws/1/accountLogin HTTP/1.1" 302 51
DEBUG:icloudpy.base:Saved session data to file
DEBUG:icloudpy.base:Cookies saved to C:\Users\zhouy\AppData\Local\Temp\icloudpy\zhouy\zy2658970293gmailcom
DEBUG:icloudpy.base.http:{'domainToUse': 'iCloud.com.cn'}
Traceback (most recent call last):
  File "C:\code\other\photoManageBack\login.py", line 8, in <module>
    api = ICloudPyService('zy2658970293@gmail.com', '**********',setup_endpoint="https://setup.icloud.com.cn/setup/ws/1")
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\zhouy\AppData\Local\Programs\Python\Python311\Lib\site-packages\icloudpy\base.py", line 278, in __init__
    self.authenticate()
  File "C:\Users\zhouy\AppData\Local\Programs\Python\Python311\Lib\site-packages\icloudpy\base.py", line 349, in authenticate
    self._webservices = self.data["webservices"]
                        ~~~~~~~~~^^^^^^^^^^^^^^^
KeyError: 'webservices'

进程已结束,退出代码1
zhouyi619 commented 1 year ago

I think I found the problem. the "Origin" Header needs to be set to "https://www.icloud.com.cn" otherwise Apple will return a 302

mandarons commented 1 year ago

@zhouyi619 please feel free to submit a PR, if you can.