我遵循 23andMe API 身份验证指南,并使用以下代码将我的用户发送到 23andMe 进行身份验证和授权:
<?php
function base64url_encode($data)
{
$b64 = base64_encode($data);
if ($b64 === false) {
return false;
}
$url = strtr($b64, '+/', '-_');
return rtrim($url, '=');
}
$client_id = 'xxx';
$redirect_uri = 'https://customURL';
// this is needed for custom redirect scheme
$code_verifier = 'yyy';
$hash = hash('sha256', $code_verifier);
$code_challenge = base64url_encode(pack('H*', $hash));
header("Location: https://api.23andme.com/authorize/"
. "?redirect_uri=$redirect_uri"
. "&response_type=code"
. "&client_id=$client_id"
. "&scope=basic"
. "&code_challenge=$code_challenge");
?>
使用 Chrome DevTools,一旦访问上述页面,我可以看到以下 3 个 URL,并且用户授权访问:
https://auth.23andme.com/authorize/?scope=openid&response_type=code&client_id=api&redirect_uri=https%3A%2F%2Fapi.23andme.com%2Foauth_callback%2F
https://api.23andme.com/oauth_callback/?code=30edd45deb5f42d4bb7bd8413866cfc5&state=
https://api.23andme.com/authorize/?response_type=code&client_id=xxx&redirect_uri=https://customURL&scope=basic&code_challenge=wAYGGEPHpGhaG1gZTmyJ8M1Ly7JlGuoUVWBBVJ4OxTU
所以我可以看到已经生成了 code 参数(在这种情况下给定了 value 30edd45deb5f42d4bb7bd8413866
。这个值应该给下面的代码(as $_GET["code"]
),但它会导致 500 错误:
<?php
$code = htmlspecialchars($_GET["code"]);
$code_verifier = 'yyy';
$post_field_array = array(
'client_id' => 'xxx',
'client_secret' => 'zzz',
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => 'https://customURL',
'scope' => 'basic',
'code_verifier' => $code_verifier);
// Encode the field values for HTTP.
$post_fields = '';
foreach ($post_field_array as $key => $value)
$post_fields .= "$key=" . urlencode($value) . '&';
$post_fields = rtrim($post_fields, '&');
// Use cURL to get the JSON response from 23andMe.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.23andme.com/token/');
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POST, count($post_field_array));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$encoded_json = curl_exec($ch);
$response = json_decode($encoded_json, true);
$access_token = $response['access_token'];
//show
print_r($encoded_json);
echo $access_token;
?>
现在 500 的响应是:
https://api.23andme.com/authorize_check?response_type=code&client_id=xxx&redirect_uri=https%3A%2F%2FcustomURL&scope=basic&select_profile=false
感谢任何有关故障排除的建议。