diff --git a/README.rst b/README.rst index 5d5b98fe..8d7a8791 100644 --- a/README.rst +++ b/README.rst @@ -47,6 +47,13 @@ Authentication without using a saved password is as simple as passing your usern In the event that the username/password combination is invalid, a ``PyiCloudFailedLoginException`` exception is thrown. +If the country/region setting of your Apple ID is China mainland, you should pass ``china_mainland=True`` to the ``PyiCloudService`` class: + +.. code-block:: python + + from pyicloud import PyiCloudService + api = PyiCloudService('jappleseed@apple.com', 'password', china_mainland=True) + You can also store your password in the system keyring using the command-line tool: .. code-block:: console diff --git a/pyicloud/base.py b/pyicloud/base.py index 6ac8bdbd..81c91807 100644 --- a/pyicloud/base.py +++ b/pyicloud/base.py @@ -212,7 +212,15 @@ def __init__( verify=True, client_id=None, with_family=True, + china_mainland=False, ): + # If the country or region setting of your Apple ID is China mainland. + # See https://support.apple.com/en-us/HT208351 + if china_mainland: + self.AUTH_ENDPOINT = "https://idmsa.apple.com.cn/appleauth/auth" + self.HOME_ENDPOINT = "https://www.icloud.com.cn" + self.SETUP_ENDPOINT = "https://setup.icloud.com.cn/setup/ws/1" + if password is None: password = get_password_from_keyring(apple_id) diff --git a/pyicloud/cmdline.py b/pyicloud/cmdline.py index 4be65b25..12b23863 100644 --- a/pyicloud/cmdline.py +++ b/pyicloud/cmdline.py @@ -52,6 +52,13 @@ def main(args=None): "fetched from the system keyring." ), ) + parser.add_argument( + "--china-mainland", + action="store_true", + dest="china_mainland", + default=False, + help="If the country/region setting of the Apple ID is China mainland" + ) parser.add_argument( "-n", "--non-interactive", @@ -168,6 +175,7 @@ def main(args=None): username = command_line.username password = command_line.password + china_mainland = command_line.china_mainland if username and command_line.delete_from_keyring: utils.delete_password_in_keyring(username) @@ -188,7 +196,7 @@ def main(args=None): parser.error("No password supplied") try: - api = PyiCloudService(username.strip(), password.strip()) + api = PyiCloudService(username.strip(), password.strip(), china_mainland=china_mainland) if ( not utils.password_exists_in_keyring(username) and command_line.interactive diff --git a/tests/__init__.py b/tests/__init__.py index 40317b20..8008eafe 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -169,9 +169,10 @@ def __init__( verify=True, client_id=None, with_family=True, + china_mainland=False, ): """Set up pyicloud service mock.""" base.PyiCloudSession = PyiCloudSessionMock base.PyiCloudService.__init__( - self, apple_id, password, cookie_directory, verify, client_id, with_family + self, apple_id, password, cookie_directory, verify, client_id, with_family, china_mainland )