iCloud Binding Communication Error

Is the problem with more than one bridge solved yet?

I still use

235 x Active x 80 x 3.3.0.202211101645 x openHAB Add-ons :: Bundles :: iCloud Binding

because I really need two working bridges.

No, this is not solved afaik.
But using family share with three AppleIDs works like a charm.

For me the “Find my Phone” function works only for the main account, but not for family members. It´s working when triggered from my iPhone. The logs show:

2023-01-27 20:40:28.281 [ERROR] [hab.core.internal.common.AbstractInvocationHandler] - An error occurred while calling method 'ThingHandler.handleCommand()' on 'org.openhab.binding.icloud.internal.handler.ICloudDeviceHandler@889478': Retry finally failed.
org.openhab.binding.icloud.internal.RetryException: Retry finally failed.
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.callApiWithRetryAndExceptionHandling(ICloudAccountBridgeHandler.java:216) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.findMyDevice(ICloudAccountBridgeHandler.java:290) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudDeviceHandler.handleCommand(ICloudDeviceHandler.java:145) ~[?:?]
	at jdk.internal.reflect.GeneratedMethodAccessor391.invoke(Unknown Source) ~[?:?]
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
	at java.lang.reflect.Method.invoke(Unknown Source) ~[?:?]
	at org.openhab.core.internal.common.AbstractInvocationHandler.invokeDirect(AbstractInvocationHandler.java:154) [bundleFile:?]
	at org.openhab.core.internal.common.InvocationHandlerSync.invoke(InvocationHandlerSync.java:59) [bundleFile:?]
	at com.sun.proxy.$Proxy11169.handleCommand(Unknown Source) [?:?]
	at org.openhab.core.thing.internal.profiles.ProfileCallbackImpl.handleCommand(ProfileCallbackImpl.java:85) [bundleFile:?]
	at org.openhab.core.thing.internal.profiles.SystemDefaultProfile.onCommandFromItem(SystemDefaultProfile.java:48) [bundleFile:?]
	at jdk.internal.reflect.GeneratedMethodAccessor390.invoke(Unknown Source) ~[?:?]
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
	at java.lang.reflect.Method.invoke(Unknown Source) ~[?:?]
	at org.openhab.core.internal.common.AbstractInvocationHandler.invokeDirect(AbstractInvocationHandler.java:154) [bundleFile:?]
	at org.openhab.core.internal.common.Invocation.call(Invocation.java:52) [bundleFile:?]
	at java.util.concurrent.FutureTask.run(Unknown Source) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:?]
	at java.lang.Thread.run(Unknown Source) [?:?]
Caused by: org.openhab.binding.icloud.internal.ICloudApiResponseException: Request https://p24-fmipweb.icloud.com:443/fmipservice/client/web/playSound failed with 500.
	at org.openhab.binding.icloud.internal.ICloudSession.request(ICloudSession.java:148) ~[?:?]
	at org.openhab.binding.icloud.internal.ICloudSession.post(ICloudSession.java:98) ~[?:?]
	at org.openhab.binding.icloud.internal.FindMyIPhoneServiceManager.playSound(FindMyIPhoneServiceManager.java:77) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.lambda$5(ICloudAccountBridgeHandler.java:294) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.callApiWithRetryAndExceptionHandling(ICloudAccountBridgeHandler.java:185) ~[?:?]
	... 19 more

Any hint for me what the problem might be? Seems to be a problem with iCloud calls. I’m using version 3.4.0 of the binding

Did not use „Find my Phone“ through openHAB, will give it a try over the weekend.

The result of the script is:

Setup Time Zone
Py iCloud Services
Two-factor authentication required.
Enter the code you received of one of your approved devices:525042
Code validation result: True
Location
None

So 2FA is working here.

The snap shot I use works fine with more than one bridge.

Family sharing works with both bridges too.

But let’s say: 2 families, I still need two bridges

:joy:

Maybe the snapshot helps solve this in actual binding?

Which SNAPSHOT ?

This one.

Was tricky with first 2FA after installation (trick was cleaning cache and putting bridges in two different text files if configuration is by file) but it works well. A few times a day bridge went offline, but come up again within next 5-7 minutes . Nothing disturbing me

Edit: find my phone I didn’t tried, but location and manual update works

Interesstingly. So there must be a difference in the API usage. Could you increase logging for pyicloud and post which requests are made (incl http headers, payload, …).

I deleted the very long return of userinfos:

30-Jan-23 18:26:30 s - DEBUG - Using session file /tmp/pyicloud/root/xxxxxxxxxxxx.session
30-Jan-23 18:26:30 s - DEBUG - Read cookies from /tmp/pyicloud/root/xxxxxxxxxxxx
30-Jan-23 18:26:30 s - DEBUG - Checking session token validity
30-Jan-23 18:26:30 s - DEBUG - Checking session token validity
30-Jan-23 18:26:30 s - DEBUG - POST https://setup.icloud.com/setup/ws/1/validate null
30-Jan-23 18:26:30 s - DEBUG - Starting new HTTPS connection (1): setup.icloud.com:443
30-Jan-23 18:26:30 s - DEBUG - https://setup.icloud.com:443 "POST /setup/ws/1/validate HTTP/1.1" 200 1839
30-Jan-23 18:26:30 s - DEBUG - Saved session data to file
30-Jan-23 18:26:30 s - DEBUG - Cookies saved to /tmp/pyicloud/root/xxxxxxxxxxxx
30-Jan-23 18:26:30 s - DEBUG - Session token is still valid
30-Jan-23 18:26:30 s - DEBUG - Authentication completed successfully
30-Jan-23 18:26:30 s - DEBUG - Starting new HTTPS connection (1): p39-fmipweb.icloud.com:443
30-Jan-23 18:26:31 s - DEBUG - https://p39-fmipweb.icloud.com:443 "POST /fmipservice/client/web/refreshClient HTTP/1.1" 200 None
30-Jan-23 18:26:31 s - DEBUG - Saved session data to file
30-Jan-23 18:26:31 s - DEBUG - Cookies saved to /tmp/pyicloud/root/xxxxxxxxxxxx
30-Jan-23 18:26:32 s - DEBUG - https://p39-fmipweb.icloud.com:443 "POST /fmipservice/client/web/refreshClient HTTP/1.1" 200 None
30-Jan-23 18:26:32 s - DEBUG - Saved session data to file
30-Jan-23 18:26:32 s - DEBUG - Cookies saved to /tmp/pyicloud/root/xxxxxxxxxxxx

Could you try to increase logging, so that we see the complete request including headers and body?
Especially for the call to ‘https://setup.icloud.com/setup/ws/1/validate’. There things are falling in your case in openhab, compared to pyicloud:

I sent you a message with a link to the complete logfile

I had a look on your logs. There is an interesting finding:

31-Jan-23 18:22:55 s - DEBUG - https://idmsa.apple.com:443 "POST /appleauth/auth/verify/trusteddevice/securitycode HTTP/1.1" 412 None
31-Jan-23 18:22:55 s - DEBUG - Saved session data to file
31-Jan-23 18:22:55 s - DEBUG - Cookies saved to /tmp/pyicloud/root/welfrogalskitonlinede
31-Jan-23 18:22:55 s - DEBUG - {'trustedDeviceCount': 3,...}, 'restrictedAccount': False, 'hsa2Account': True, 'managedAccount': False, 'supportsRecovery': True}, 'aboutTwoFactorAuthenticationUrl': 'https://support.apple.com/kb/HT204921'}
31-Jan-23 18:22:55 s - DEBUG - Code verification successful.

The http return code 421 is treated as error in the current binding implementation. For some reason, pyicloud treats that as successful (even if 4xx are ERRORS). In pyicoud there is some special handling.

        try:
            self.session.post(
                "%s/verify/trusteddevice/securitycode" % self.AUTH_ENDPOINT,
                data=json.dumps(data),
                headers=headers,
            )
        except PyiCloudAPIResponseException as error:
            if error.code == -21669:
                # Wrong verification code
                LOGGER.error("Code verification failed.")
                return False
            raise

It would need some time to understand why is this done and reimplement it in the openhab binding. But it is a way forward.

@WeRo I sent you a message with a test version. Please report the results here.

Still doesn’t work - now it’s code 412

2023-02-02 19:53:09.846 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - Starting iCloud authentication (AuthState=INITIAL, Thing=icloud:account:8a8a28b81c)...
2023-02-02 19:53:09.848 [DEBUG] [inding.icloud.internal.ICloudService] - Authenticating as XXXXXXXXXXXX...
2023-02-02 19:53:09.867 [DEBUG] [inding.icloud.internal.ICloudSession] - iCloud request POST https://idmsa.apple.com/appleauth/auth/signin?isRememberMeEnabled=true.
2023-02-02 19:53:11.907 [DEBUG] [inding.icloud.internal.ICloudSession] - iCloud request POST https://setup.icloud.com/setup/ws/1/accountLogin.
2023-02-02 19:53:13.156 [WARN ] [l.handler.ICloudAccountBridgeHandler] - iCloud authentication requires 2-FA code. Please provide code configuration for thing 'icloud:account:8a8a28b81c'.
2023-02-02 19:53:36.938 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - iCloud bridge handler initializing ...
2023-02-02 19:53:36.942 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - Starting iCloud 2-FA authentication  AuthState=WAIT_FOR_CODE, Thing=icloud:account:8a8a28b81c)...
2023-02-02 19:53:36.942 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - iCloud bridge handler initialized.
2023-02-02 19:53:36.946 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - Code is given in thing configuration 'icloud:account:8a8a28b81c'. Trying to validate code...
2023-02-02 19:53:36.950 [DEBUG] [inding.icloud.internal.ICloudSession] - iCloud request POST https://idmsa.apple.com/appleauth/auth/verify/trusteddevice/securitycode.
2023-02-02 19:53:37.433 [DEBUG] [inding.icloud.internal.ICloudService] - Code verification failed.
org.openhab.binding.icloud.internal.ICloudApiResponseException: Request https://idmsa.apple.com/appleauth/auth/verify/trusteddevice/securitycode failed with 412.
	at org.openhab.binding.icloud.internal.ICloudSession.request(ICloudSession.java:148) ~[?:?]
	at org.openhab.binding.icloud.internal.ICloudSession.post(ICloudSession.java:98) ~[?:?]
	at org.openhab.binding.icloud.internal.ICloudService.validate2faCode(ICloudService.java:254) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.handle2FAAuthentication(ICloudAccountBridgeHandler.java:244) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.callApiWithRetryAndExceptionHandling(ICloudAccountBridgeHandler.java:170) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.lambda$2(ICloudAccountBridgeHandler.java:130) ~[?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
	at java.lang.Thread.run(Thread.java:829) [?:?]
2023-02-02 19:53:37.442 [WARN ] [l.handler.ICloudAccountBridgeHandler] - ICloud token invalid.
2023-02-02 19:54:42.353 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - iCloud bridge handler initializing ...
2023-02-02 19:54:42.355 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - iCloud bridge handler initialized.
2023-02-02 19:54:42.358 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - Starting iCloud authentication (AuthState=INITIAL, Thing=icloud:account:8a8a28b81c)...
2023-02-02 19:54:42.367 [DEBUG] [inding.icloud.internal.ICloudService] - Checking session token validity
2023-02-02 19:54:42.370 [DEBUG] [inding.icloud.internal.ICloudSession] - iCloud request POST https://setup.icloud.com/setup/ws/1/validate.
2023-02-02 19:54:42.679 [DEBUG] [inding.icloud.internal.ICloudService] - Token is not valid. Attemping new login.
org.openhab.binding.icloud.internal.ICloudApiResponseException: Request https://setup.icloud.com/setup/ws/1/validate failed with 421.
	at org.openhab.binding.icloud.internal.ICloudSession.request(ICloudSession.java:148) ~[?:?]
	at org.openhab.binding.icloud.internal.ICloudSession.post(ICloudSession.java:98) ~[?:?]
	at org.openhab.binding.icloud.internal.ICloudService.validateToken(ICloudService.java:195) ~[?:?]
	at org.openhab.binding.icloud.internal.ICloudService.authenticate(ICloudService.java:96) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.checkLogin(ICloudAccountBridgeHandler.java:341) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.callApiWithRetryAndExceptionHandling(ICloudAccountBridgeHandler.java:167) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.lambda$2(ICloudAccountBridgeHandler.java:130) ~[?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
	at java.lang.Thread.run(Thread.java:829) [?:?]
2023-02-02 19:54:42.690 [DEBUG] [inding.icloud.internal.ICloudService] - Authenticating as XXXXXXXXXXXXX...
2023-02-02 19:54:42.692 [DEBUG] [inding.icloud.internal.ICloudSession] - iCloud request POST https://idmsa.apple.com/appleauth/auth/signin?isRememberMeEnabled=true.
2023-02-02 19:54:43.834 [DEBUG] [inding.icloud.internal.ICloudSession] - iCloud request POST https://setup.icloud.com/setup/ws/1/accountLogin.
2023-02-02 19:54:44.888 [WARN ] [l.handler.ICloudAccountBridgeHandler] - iCloud authentication requires 2-FA code. Please provide code configuration for thing 'icloud:account:8a8a28b81c'.
2023-02-02 19:55:08.345 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - iCloud bridge handler initializing ...
2023-02-02 19:55:08.349 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - Starting iCloud 2-FA authentication  AuthState=WAIT_FOR_CODE, Thing=icloud:account:8a8a28b81c)...
2023-02-02 19:55:08.351 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - iCloud bridge handler initialized.
2023-02-02 19:55:08.351 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - Code is given in thing configuration 'icloud:account:8a8a28b81c'. Trying to validate code...
2023-02-02 19:55:08.354 [DEBUG] [inding.icloud.internal.ICloudSession] - iCloud request POST https://idmsa.apple.com/appleauth/auth/verify/trusteddevice/securitycode.
2023-02-02 19:55:08.819 [DEBUG] [inding.icloud.internal.ICloudService] - Code verification failed.
org.openhab.binding.icloud.internal.ICloudApiResponseException: Request https://idmsa.apple.com/appleauth/auth/verify/trusteddevice/securitycode failed with 412.
	at org.openhab.binding.icloud.internal.ICloudSession.request(ICloudSession.java:148) ~[?:?]
	at org.openhab.binding.icloud.internal.ICloudSession.post(ICloudSession.java:98) ~[?:?]
	at org.openhab.binding.icloud.internal.ICloudService.validate2faCode(ICloudService.java:254) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.handle2FAAuthentication(ICloudAccountBridgeHandler.java:244) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.callApiWithRetryAndExceptionHandling(ICloudAccountBridgeHandler.java:170) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.lambda$2(ICloudAccountBridgeHandler.java:130) ~[?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
	at java.lang.Thread.run(Thread.java:829) [?:?]

Ok, sent you another version which only treats 400 as invalid code.

yep that’s it :+1:

You mean it‘s working now?

Yes it’s working perfectly now - with two accounts and all devices of one family share

That’s great news, I will create PR to include this fix in the next OH release and provide a RC on GitHub, for other users.