使用Apple ASAuthorizationAppleIDCredential登录在iOS的电子邮件字段中返回`nil`

piok6c0g  于 2023-05-19  发布在  iOS
关注(0)|答案(1)|浏览(422)

当用户第一次使用“使用Apple登录”登录时,ASAuthorizationAppleIDCredential将在“电子邮件”字段中返回正确的电子邮件地址。但是,在使用相同的Apple ID注销并再次登录后,ASAuthorizationAppleIDCredential在“email”字段中给出nil。
如果第一次“使用Apple登录”成功,但由于某种原因,使用我们自己的服务器注册失败,该怎么办?第二次我们将无法获得用户的电子邮件。

dm7nw8vv

dm7nw8vv1#

id_token中通过web(https://appleid.apple.com/auth/authorize + https://appleid.apple.com/auth/token)使用Apple SignIn时,您将始终有可用的电子邮件,并且只有用户首次登录时才有可用的用户名,但在id_token中没有,而是在重定向到您的redirect_uri时在$_REQUEST[“user”]中。
另一方面,当使用Apple SignIn通过Swift时,您将只有第一次可用的用户名和电子邮件。因此,如果你想避免@Harsha的询问评论中提到的解决方案(使用用户标识符和电子邮件对创建内部表),你可以在Swift中获取autorizationCode,将其传输到您的后端/服务器并继续进行Web登录-向https://appleid.apple.com/auth/token发出请求,您将拥有id_token,电子邮件字段可用于每次登录,而不仅仅是第一次登录。这就是我们解决问题的方法。然而,在我们的案例中,我们还为本地应用程序的网站版本实现了网络登录,所以这没有问题。
小心点...如果web登录client_id是来自https://developer.apple.com/account/resources/identifiers/list/serviceId的服务标识符+例如由lib https://github.com/gradus0/appleAuth创建的client_secret。但是,如果本地应用client_id是必须在这里注册的应用程序budle标识符https://developer.apple.com/account/resources/identifiers/list/bundleId+其client_secret,例如与前面提到的相同的lib。

let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential,
let authCodeData = appleIDCredential.authorizationCode,
let authCodeString = String(data: authCodeData, encoding: .utf8) {
    // send authCodeString to your backend/server and continue as for web sign-in
    // so you must make request to https://appleid.apple.com/auth/token to get id_token
    // with user email
}

相关问题