简体   繁体   English

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

[英]How to get user's name on Sign in with Apple?

I'm trying to authenticate users with Apple on the web (which is required as we also have an iOS app and have a Login with Facebook option and we have to bring Sign in with Apple too otherwise Apple rejects our app updates on iOS side) and here is my meta tags (with sensitive info redacted) as in Apple's example: I'm trying to authenticate users with Apple on the web (which is required as we also have an iOS app and have a Login with Facebook option and we have to bring Sign in with Apple too otherwise Apple rejects our app updates on iOS side)这是我的元标记(敏感信息已编辑),如 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">

When the user taps the button it asks for sharing a name and/or email correctly:当用户点击按钮时,它要求正确共享名称和/或 email:

在此处输入图像描述

I proceed, and my redirect URL is called as expected.我继续,我的重定向 URL 按预期调用。 However, when I decode the returned id_token I see only email and email_verified fields in addition to the JWT-related fields.但是,当我解码返回的id_token时,除了 JWT 相关字段之外,我只看到emailemail_verified字段。 I don't have anything related to the user's name.我没有任何与用户名相关的信息。 I know that the name field is populated only on the first auth so this is after a fresh login after removing my app from my Apple ID from the "Apps & Websites using Apple ID" page.我知道名称字段仅在第一次身份验证时填充,因此这是在从“使用 Apple ID 的应用程序和网站”页面从我的 Apple ID 中删除我的应用程序后重新登录后。 If I change appleid-signin-scope on meta tags to name I don't get an email either, so that value is somehow being respected.如果我将元标记上appleid-signin-scope更改为name ,我也不会得到 email,因此该值在某种程度上得到了尊重。 I also haven't touched anything regarding the name field as in the screenshot (I could proceed by Touch ID without even clicking anything) yet the id_token can only have email, not name.我也没有触及屏幕截图中关于名称字段的任何内容(我可以通过 Touch ID 进行操作,甚至无需单击任何内容)但id_token只能具有 email,而不是名称。 I need to get users' names in addition to email in order to finish registration, and I'm forced to add Sign-in with the Apple option to push any updates to my iOS app.除了 email 之外,我还需要获取用户的姓名才能完成注册,并且我不得不使用 Apple 选项添加登录以将任何更新推送到我的 iOS 应用程序。 Many people have reported that they get the name only the first time (as clearly stated by Apple), but I'm not getting it even on the first time.许多人报告说他们只是第一次得到这个名字(正如苹果明确指出的那样),但我什至第一次都没有得到它。

What am I doing wrong?我究竟做错了什么?

The code needs to request the UserI property, as specified in the Sign in with Apple API documentation.代码需要请求UserI属性,如Sign in with Apple API文档中所述。

Sample:样本:

<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">

文档图片

Unfortunately, it seems this isn't possible.不幸的是,这似乎是不可能的。 However the name is returned as JSON in the response body from the "authorization" endpoint along with the id token.但是,该名称在来自“授权”端点的响应正文中与 id 令牌一起返回为 JSON。 This is only available the first time a user signs in, when they give consent for your application to access their name and email address, not on subsequent sign-ins.这仅在用户第一次登录时可用,当他们同意您的应用程序访问他们的姓名和 email 地址时,在后续登录时不可用。

Apple documentation - Retrieve the User's Information Apple 文档 - 检索用户信息

If you request the user's full name, Sign in with Apple collects the information to pass along to your app.如果您请求用户的全名,Sign in with Apple 会收集信息以传递给您的应用程序。 The name defaults to the user's name from their Apple ID, but the user can change their name.该名称默认为用户 Apple ID 中的名称,但用户可以更改其名称。 The modified name is only shared with your app and not with Apple, and hence isn't included in the ID token.修改后的名称仅与您的应用共享,而不与 Apple 共享,因此不包含在 ID 令牌中。

Response from "https://appleid.apple.com/auth/authorize" including "user" object:来自“https://appleid.apple.com/auth/authorize”的响应,包括“用户”object:

{
  "code": "AUTH_CODE",
  "id_token": "ID_TOKEN",
  "user": {
    "name": {
      "firstName": "Jeb",
      "lastName": "Kerman"
    },
    "email": "jeb.kerman@apple.com"
  }
}

Apple documentation - Handle authorization Response Apple 文档 - 处理授权响应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM