Skip to content

Commit

Permalink
Should use different endpoint if the country/region setting of the Ap…
Browse files Browse the repository at this point in the history
…ple ID is China mainland. (#418)

* If the country or region setting of Apple ID is China mainland use different endpoint.

* Add command line option `--china-mainland`

* Documentation added to README
  • Loading branch information
qzchenwl authored Oct 25, 2024
1 parent 332cc9f commit 622cd16
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pyicloud/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 9 additions & 1 deletion pyicloud/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit 622cd16

Please sign in to comment.