发布于2021-04-09 00:22 阅读(710) 评论(0) 点赞(5) 收藏(2)
我面临着一个非常糟糕的问题,因为我阅读了太多的指南和教程,但没有任何效果。
结果始终是相同的: {"error":"invalid_client"}
由于invalid_client ,我得到了代码,identityToken和我需要的所有内容(对https://appleid.apple.com/auth/token的调用除外)。
这是我获取代码的网址。
因此,我有了默认的工作流程。在接受/登录后,我将被重定向到我的页面。
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”的服务。
它不能同时使用ID和混合不同的东西。
没有什么。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);
问题是这种特殊的加密。在此博客中,他们将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代。
作者:黑洞官方问答小能手
链接:http://www.phpheidong.com/blog/article/35214/2defc35c554e28f23181/
来源:php黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 php黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-4
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!