Apple seems to be revoking tokens before their expiration dates. Several
users reporting problems on issue [fastlane#19072][1] were likely affected by
it. Sometimes changing the token duration will work around the problem,
as the tokens are likely being revoked later in the future and allowing
the wait to complete.
This can be seen in practice by adding a `@token.refresh!` couple of extra log
messages to [api_client.rb][2] right at the point where we get an
`UnauthorizedAccessError`:
```
INFO [2021-10-19 18:38:31.74]: Successfully uploaded the new binary to App Store Connect
INFO [2021-10-19 18:38:31.74]: If you want to skip waiting for the processing to be finished, use the `skip_waiting_for_build_processing` option
INFO [2021-10-19 18:38:31.74]: Note that if `skip_waiting_for_build_processing` is used but a `changelog` is supplied, this process will wait for the build to appear on AppStoreConnect, update the changelog and then skip the remaining of the processing steps.
DEBUG [2021-10-19 18:38:31.82]: App Platform (ios)
INFO [2021-10-19 18:38:31.92]: Waiting for processing on... app_id: 1370986669, app_version: 2.23.2, build_version: 262025, platform: IOS
WARN [2021-10-19 18:38:32.26]: Read more information on why this build isn't showing up yet - fastlane#14997
INFO [2021-10-19 18:38:32.26]: Waiting for the build to show up in the build list - this may take a few minutes (check your email for processing issues if this continues)
INFO [2021-10-19 18:39:02.70]: Waiting for the build to show up in the build list - this may take a few minutes (check your email for processing issues if this continues)
token expired?: false - expiration: 2021-10-19 18:45:04 -0400
Token has expired, has been revoked, or is invalid! Trying to refresh
INFO [2021-10-19 18:39:33.26]: Waiting for the build to show up in the build list - this may take a few minutes (check your email for processing issues if this continues)
INFO [2021-10-19 18:40:03.69]: Waiting for the build to show up in the build list - this may take a few minutes (check your email for processing issues if this continues)
INFO [2021-10-19 18:40:34.33]: Waiting for the build to show up in the build list - this may take a few minutes (check your email for processing issues if this continues)
INFO [2021-10-19 18:41:04.76]: Waiting for the build to show up in the build list - this may take a few minutes (check your email for processing issues if this continues)
INFO [2021-10-19 18:41:35.21]: Waiting for the build to show up in the build list - this may take a few minutes (check your email for processing issues if this continues)
INFO [2021-10-19 18:42:05.67]: Waiting for the build to show up in the build list - this may take a few minutes (check your email for processing issues if this continues)
token expired?: false - expiration: 2021-10-19 18:47:52 -0400
Token has expired, has been revoked, or is invalid! Trying to refresh
INFO [2021-10-19 18:42:36.34]: Waiting for the build to show up in the build list - this may take a few minutes (check your email for processing issues if this continues)
INFO [2021-10-19 18:43:06.80]: Waiting for the build to show up in the build list - this may take a few minutes (check your email for processing issues if this continues)
INFO [2021-10-19 18:43:37.23]: Waiting for the build to show up in the build list - this may take a few minutes (check your email for processing issues if this continues)
```
The logs above show that, even though the current token is not expired, the
appconnect API responds with a 401 and the process would otherwise fail.
This refreshes the token and adds retries to `UnauthorizedAccessError`
before ultimately raising the error if it still continues to fail.
[1]: fastlane#19072
[2]: https://github.com/fastlane/fastlane/blob/62af236780a9eace9d0487d0767f50d1a17a1c6c/spaceship/lib/spaceship/connect_api/api_client.rb#L172-L175