10

我面临一个非常糟糕的问题,因为我阅读了很多指南和教程,但没有任何效果。

结果总是一样的:{"error":"invalid_client"}

我得到了代码、identityToken 和我需要的一切——除了对https://appleid.apple.com/auth/token的调用——因为 invalid_client。

这是我获取代码的网址。

https://appleid.apple.com/auth/authorize?response_type=code&client_id=org.example.service&redirect_uri=https%3A%2F%2Fexample.org

所以我有默认的工作流程。在接受/登录后,我将被重定向到我的页面。

https://example.org/?code=a277243e2ec324fb09ba1c3333a8e6576.0.abcde.u4xiTDP2qHXoNEaxrcrIGx

(当我使用 JavaScript API 时,我会得到其他信息,如状态、代码和 id_token。我也已经用那里的“代码”试过了。)

回到主函数。

这是我对苹果的要求。

'client_id' => 'org.example.service',  
'client_secret' => JWT-Data encoded (OPENSSL_ALGO_SHA256) see below  
'grant_type' => 'authorization_code',  
'code' => 'a277243e2ec324fb09ba1c3333a8e6576.0.abcde.u4xiTDP2qHXoNEaxrcrIGx'  

智威汤逊标题:

{
  "alg": "ES256",
  "kid": "1ABC2345DE"
}  

智威汤逊有效载荷:

{
  "iss": "1A234BCD56",
  "iat": 1571269964,
  "exp": 1571273564,
  "aud": "https://appleid.apple.com",
  "sub": "org.example.service"
}

回复:

{  
  "error": "invalid_client"  
}  

世界无用的错误信息。

我不知道为什么客户端应该是无效的。

我在https://developer.apple.com/account/resources/authkeys/list中有一个密钥,下载的文件名为 AuthKey_1ABC2345DE.p8。(表示 1ABC2345DE 是我的密钥 ID)

然后我有一个标识符为“org.example”的本机 iOS 应用程序和一个标识符为“org.example.service”的服务。

它不能同时使用 id 和混合不同的东西。

没有。无效客户端。

任何人都可以帮助我吗?我在这里坐了几个小时,只得到了 invalid_client

我的测试页面:

<html>
<head>
</head>
<body>
<script type="text/javascript" src="https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js"></script>
<div id="appleid-signin" data-color="black" data-border="true" data-type="sign in" data-width="330px" data-height="100px"></div>
<script type="text/javascript">
    AppleID.auth.init({
        clientId : 'org.example.service',
        scope : 'email',
        redirectURI: 'https://example.org',
        state : 'EN'
    });
</script>
</body>
</html>

和 PHP:

<?php
// index.php

// function by https://stackoverflow.com/q/56459075/1362858
function encode($data) {
    $encoded = strtr(base64_encode($data), '+/', '-_');
    return rtrim($encoded, '=');
}

// function by https://stackoverflow.com/q/56459075/1362858
function generateJWT($kid, $iss, $sub, $key) {
    $header = [
        'alg' => 'ES256',
        'kid' => $kid
    ];
    $body = [
        'iss' => $iss,
        'iat' => time(),
        'exp' => time() + 3600,
        'aud' => 'https://appleid.apple.com',
        'sub' => $sub
    ];

    $privKey = openssl_pkey_get_private($key);
    if (!$privKey) return false;

    $payload = encode(json_encode($header)).'.'.encode(json_encode($body));
    $signature = '';
    $success = openssl_sign($payload, $signature, $privKey, OPENSSL_ALGO_SHA256);
    if (!$success) return false;

    return $payload.'.'.encode($signature);
}

$client_id = 'org.example.service';
$data = [
    'client_id' => $client_id,
    'client_secret' => generateJWT('1ABC2345DE', '1A234BCD56', $client_id, file_get_contents('AuthKey_1ABC2345DE.p8')),
    'code' => 'a277243e2ec324fb09ba1c3333a8e6576.0.abcde.u4xiTDP2qHXoNEaxrcrIGx',
    'grant_type' => 'authorization_code'
];
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://appleid.apple.com/auth/token');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$serverOutput = curl_exec($ch);

curl_close ($ch);

/**
 * {"error":"invalid_client"}
 */
var_dump($serverOutput);

4

2 回答 2

5

问题是这种特殊的加密。在这个博客中,他们使用 PHP 处理除了 client_secret 生成之外的所有内容。 https://developer.okta.com/blog/2019/06/04/what-the-heck-is-sign-in-with-apple

在文本中作者解释了这句话:

Some JWT libraries don’t support elliptic curve methods, so make sure yours does before you start trying this out.

现在它与顶部的代码完全一致 - 只替换了 client_secret 生成。

于 2019-10-21T20:36:23.123 回答
2

好吧,我遇到了这个错误“invalid_client”并且刚刚发现,所有凭据都在https://developer.apple.com/account/resources/identifiers/serviceId中更改了。我认为,因为我们的法人实体(作为账户持有人)发生了变化。因此,只需检查您的网络应用程序 ID 和 .p8 密钥,它是否存在。

ps 很有趣,大约一年前,我在这个问题上来过这里,它对我有帮助,如上所述(JWT ES256 算法)。现在,我希望,我会用另一种解决方案帮助某人:-)

于 2021-10-08T19:10:17.287 回答