如何在使用 Apple 登录时获取用户名?

问题描述

我正在尝试使用 Apple 在网络上验证用户(这是必需的,因为我们还有一个 iOS 应用程序和一个使用 Facebook 登录的选项,我们也必须使用 Apple 登录否则 Apple 会拒绝我们在 iOS 端的应用更新),这是我的元标记(敏感信息已编辑),如 Apple 的示例所示:

<Meta name="appleid-signin-client-id" content="com.myapp">
<Meta name="appleid-signin-scope" content="name email">
<Meta name="appleid-signin-redirect-uri" content="https://my.return.url">
<Meta name="appleid-signin-state" content="x login">
<Meta name="appleid-signin-nonce" content="some number">
<Meta name="appleid-signin-use-popup" content="false">

用户点击按钮时,它会要求正确分享姓名和/或电子邮件

enter image description here

我继续,我的重定向 URL 按预期调用。但是,当我解码返回的 id_token 时,除了与 JWT 相关的字段外,我只看到 emailemail_verified 字段。我没有任何与用户名相关的信息。我知道名称字段仅在第一次身份验证时填充,因此这是在从“使用 Apple ID 的应用程序和网站”页面从我的 Apple ID 中删除我的应用程序后重新登录之后。如果我将元标记上的 appleid-signin-scope 更改为 name,我也不会收到电子邮件,因此该值以某种方式受到尊重。我也没有像屏幕截图中那样触及有关名称字段的任何内容(我可以通过 Touch ID 进行操作,甚至无需单击任何内容),但 id_token 只能有电子邮件,不能有姓名。为了完成注册,除了电子邮件之外,我还需要获取用户的姓名,并且我必须添加“使用 Apple 登录”选项以将任何更新推送到我的 iOS 应用程序。许多人报告说他们只是第一次得到这个名字(正如 Apple 明确说明的那样),但我什至第一次就没有得到它。

我做错了什么?

解决方法

代码需要请求 Sign in with Apple API 文档中指定的 UserI 属性。

示例:

<meta name="appleid-signin-client-id" content="com.myapp">
<meta name="appleid-signin-scope" content="UserI">
<meta name="appleid-signin-redirect-uri" content="https://my.return.url">
<meta name="appleid-signin-state" content="x login">
<meta name="appleid-signin-nonce" content="some number">
<meta name="appleid-signin-use-popup" content="false">

Image of documentation