尝试使用 Apple 提供商将 Apple SSO 添加到 Laravel Socialite 时出错

问题描述 投票:0回答:0

我正在尝试将 Apple 单点登录添加到我的 Web 应用程序,由 Laravel 提供支持。

我正在使用他们登录的 javascript 版本,我不喜欢它,但似乎是他们的首选方向。

按照那里的说明,我已将以下内容添加到我的登录页面:

    <meta name="appleid-signin-client-id" content="com.mywebsite.service-id">
    <meta name="appleid-signin-scope" content="email name">
    <meta name="appleid-signin-redirect-uri" content="https://app.mywebsite.com/auth/apple/callback">
    <meta name="appleid-signin-state" content="{{ csrf_token() }}">
    <meta name="appleid-signin-nonce" content="e3928c0c-edf0-11ed-a05b-madeupnonce">
    <meta name="appleid-signin-use-popup" content="true">
    ...
    <script type="text/javascript" src="https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js"></script>

此代码成功创建了一个Apple 登录弹窗,并返回一个AppleIDSignInOnSuccess 事件到当前页面(不是那个回调页面)。文档让它看起来像是预期的,我认为这里没有使用回调?

该事件包含一个带有 id_token、代码和状态的授权对象。在这一点上,我很迷茫!我尝试使用该信息将用户转移到回电。

        document.addEventListener('AppleIDSignInOnSuccess', (event) => {
            let detail = event.detail.authorization;
            window.location = '/auth/apple/callback?code=' + detail.code + 'id_token=' + detail.id_token + 'state=' + detail.state;
        });

这似乎顺利通过,但 Socialite Apple 提供商在调用 Apple 的端点后,始终返回:

客户端错误:

POST https://appleid.apple.com/auth/token
导致
400 Bad Request
回复: {"error":"invalid_grant","error_description":"代码已过期或 已被撤销。”}

这是我处理 Apple 回调的代码:

public function handleAppleCallback()
{
    echo config('services.apple.client_id'); // com.mywebsite.service-id
    echo config('services.apple.client_secret'); // LongSecretFROMinstuctionsHERE
    echo config('services.apple.redirect_uri'); // https://app.mywebsite.com/auth/apple/callback
    $appleUser = Socialite::driver('apple')
        ->stateless()
        ->user();

我通过指令 here 获得的密钥值,尽管我也尝试过创建密钥 on the fly。无论哪种方式,我都会得到相同的结果。我在这些说明中使用的配置值是:

team_id = '3Z8TYT4SL9' // my alpha numeric team id (not the real one)
client_id = 'com.sharewithleaf.service-id'
key_id = '85UX3UQVT8' // my alpha numeric key created linked to my primary app id (not the real one)

我认为这些是基于说明的正确值,但我还没有找到显示结果值示例的文档来帮助我进行完整性检查。

此时我陷入了僵局。我能想到的几种可能性:

  1. 我把整个流程弄糊涂了
  2. 我在 AppleIDSignInOnSuccess 之后将不正确的参数传递给回调
  3. 我正在创建/使用错误的 client_id 和秘密,应该使用类似 API 密钥 的东西。
  4. 我用错误的值错误地生成了密钥。

非常感谢任何帮助!

laravel laravel-socialite apple-sign-in
© www.soinside.com 2019 - 2024. All rights reserved.