我正面临着一个非常糟糕的问题,因为我读了那么多的指南和教程,什么也不管用。
结果总是一样的:{"error":"invalid_client"}
我得到了代码、identityToken和我需要的一切--除了调用https://appleid.apple.com/auth/token --是因为invalid_client。
这是我得到密码的网址。
那么我就有了默认的工作流。在接受/退出后,我将被重定向到我的页面。
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'
JWT头:
{
"alg": "ES256",
"kid": "1ABC2345DE"
}
JWT有效载荷:
{
"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”的服务。
它不能同时处理ids和混合不同的东西。
没什么。invalid_client。
有人能帮我吗?我在这里坐了几个小时,只得到了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);
发布于 2019-10-21 20:36:23
问题是这种特殊的加密。在这个博客中,除了client_secret生成之外,所有东西都使用PHP。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生成。
发布于 2021-10-08 19:10:17
嗯,我遇到了"invalid_client“这个错误,刚刚发现,https://developer.apple.com/account/resources/identifiers/serviceId中的所有凭据都被更改了。我认为,因为我们的法律实体(作为帐户持有人)的变化。所以,只要检查您的web应用程序ID和.p8键,它是否存在。
附注:有趣的是,大约一年前,我在这个问题中参与了这个问题,正如上面所描述的那样,它帮助了我(JWT ES256 algo)。现在,我希望,我能帮助别人找到另一个解决方案:-)
https://stackoverflow.com/questions/58423244
复制相似问题