我面临一个非常糟糕的问题,因为我阅读了很多指南和教程,但没有任何效果。
结果总是一样的:{"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'
智威汤逊标题:
{
"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);