使用 Apple 登录会引发 invalid_client 错误

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

我正在尝试将我的 Laravel 应用程序中的 Apple 登录与 Laravel/Socialite 集成。我已从开发者帐户创建了该应用程序并使用了所有正确的凭据。并使用私钥生成秘密密钥。 但我面临着问题

Client error: 
POST https://appleid.apple.com/auth/token` 导致了
400 Bad Request
响应:{"error":"invalid_client"}`。 我已按照 本教程 使用私有身份验证密钥生成 client_secret_key。

我读了很多指南、论坛讨论和教程,但没有任何效果。

laravel laravel-socialite apple-login
1个回答
0
投票

使用 Apple for Laravel 登录

安装

作曲家需要社交名流提供商/苹果

跟随链接:https://socialiteproviders.com/Apple/#installation-basic-usage

配置

  1. 为您的网站 (

    https://developer.apple.com/account/resources/identifiers/list/bundleId
    ) 创建一个 App ID,其中包含以下详细信息:

    • 平台:iOS、tvOS、watchOS(我不确定这两种选择是否对网络应用程序有影响)
    • 描述:(类似于“example.com 应用程序 ID”)
    • 捆绑 ID(显式):com.example.id(或类似的东西)
    • 勾选“使用 Apple 登录”
  2. 为您的网站 (

    https://developer.apple.com/account/resources/identifiers/list/serviceId
    ) 创建一个 Service ID,其中包含以下详细信息:

    • 描述:(类似于“example.com 服务 ID”)
    • 标识符:com.example.service(或类似的东西)
    • 勾选“使用 Apple 登录”
    • 配置“使用Apple登录”:
      • 主要应用程序 ID:(选择步骤 1 中创建的主要应用程序 ID)
      • 网站域名:example.com(您网站的域名)
      • 返回 URL:https://example.com/apple-signin(指向控制器中回调方法的路由)
      • 点击“保存”。
      • 点击“编辑”按钮,编辑“Sign In With Apple”的详细信息 我们刚刚创建的配置。
      • 如果您尚未验证域名,请下载验证文件, 将其上传到https://example.com/.well-known/apple-developer-domain-association.txt,然后单击“验证” 按钮。
  3. 为您的网站 (

    https://developer.apple.com/account/resources/authkeys/list
    ) 创建一个 Private Key,其中包含以下详细信息:

    • 按键名称:
    • 勾选“使用 Apple 登录”
    • 配置“使用Apple登录”:
      • 主应用程序 ID:(选择步骤 1 中创建的主应用程序 ID)
      • 点击“保存”
    • 点击“继续”
    • 点击“注册”
    • 点击“下载”
    • 将下载的文件重命名为
      key.txt
  4. 创建应用程序的客户端密钥:

    • 安装 JWT Gem:
      sudo gem install jwt
      
  5. 首先,确保您已经安装了 Ruby,然后通过从命令行运行它来安装 JWT gem。

    • 创建一个名为
      client_secret.rb
      的文件来处理私钥:
      require 'jwt'
      
      key_file = 'key.txt'
      team_id = ''
      client_id = ''
      key_id = ''
      
      ecdsa_key = OpenSSL::PKey::EC.new IO.read key_file
      
      headers = {
      'kid' => key_id
      }
      
      claims = {
          'iss' => team_id,
          'iat' => Time.now.to_i,
          'exp' => Time.now.to_i + 86400*180,
          'aud' => 'https://appleid.apple.com',
          'sub' => client_id,
      }
      
      token = JWT.encode claims, ecdsa_key, 'ES256', headers
      
      puts token
      

使用PHP

    Make Sure `composer require firebase/php-jwt` Installed 
    ``` 
    client_secret.php

    <?php

    require 'vendor/autoload.php';

    use Firebase\JWT\JWT;

    

    $teamId = 'TEAM ID';

    $keyId = 'KEY ID';

    $sub = 'com.avocado.client';

    $aud = 'https://appleid.apple.com'; // it's a fixed URL value

    $iat = strtotime('now');

    $exp = strtotime('+60days');

    $keyContent = file_get_contents('key.txt');

    echo JWT::encode([

        'iss' => $teamId,

        'iat' => $iat,

        'exp' => $exp,

        'aud' => $aud,

        'sub' => $sub,

    ], $keyContent, 'ES256', $keyId);

    // Write the snippet in a method, return the value from that method

    // You

    ?>
    ```

- Fill in the following fields:
    - `team_id`: This can be found on the top-right corner when logged into
        your Apple Developer account, right under your name.
    - `client_id`: This is the identifier from the Service Id created in step
        2 above, for example com.example.service
    - `key_id`: This is the identifier of the private key created in step 3
        above.
- Save the file and run it from the terminal. It will spit out a JWT which is
    your client secret, which you will need to add to your `.env` file in the
    next step.
    ```sh
    ruby client_secret.rb
    ```
    
  1. 在您的

    .env
    文件中设置必要的环境变量:

    APPLE_CLIENT_ID=app_Service_id
    
    

APPLE_CLIENT_SECRET=ggenerator_key_here APPLE_REDIRECT_URI=redirect_url_here ````

© www.soinside.com 2019 - 2024. All rights reserved.