无法使用23andme API进行身份验证的PHP代码
根据23andMeAPI()的规范,此脚本从与$redirect_URI相同的URL运行。但是,无论我尝试什么,脚本都不会输出任何内容。我做错了什么?我不知道为什么它不工作,但我建议您进行一些调试。从无法使用23andme API进行身份验证的PHP代码,php,23andme-api,Php,23andme Api,根据23andMeAPI()的规范,此脚本从与$redirect_URI相同的URL运行。但是,无论我尝试什么,脚本都不会输出任何内容。我做错了什么?我不知道为什么它不工作,但我建议您进行一些调试。从 <?php $client_id = "XXXXXXXXX1"; $client_secret = "XXXXXXXXXX2"; $redirect_URI = "XXXXXXXXX3"; $auth_code = htmlspecialchars($_GET["code"]); $po
<?php
$client_id = "XXXXXXXXX1";
$client_secret = "XXXXXXXXXX2";
$redirect_URI = "XXXXXXXXX3";
$auth_code = htmlspecialchars($_GET["code"]);
$post_field_array = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'grant_type' => 'authorization_code',
'code' => $auth_code,
'redirect_uri' => $redirect_uri,
'scope' => 'basic genomes');
$post_fields = '';
foreach ($post_field_array as $key => $value)
$post_fields .= "$key=" . urlencode($value) . '&';
$post_fields = rtrim($post_fields, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.23andme.com/token/');
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'];
print $access_token;
?>
(或者使用var_dump)并查看其输出可能是什么。curl_exec失败了吗
尝试将verbose标志设置为curl,看看这是否会引发任何错误(警告),从而导致出现问题
print_r($encoded_json)
首先,我收到这3张通知, 除非我在querystring上传递代码,否则代码不存在,重定向\u uri在两种用法中的大小写不同,access\u令牌可能不存在,因为发生了身份验证错误 注意:未定义索引:第6行test.php中的代码 注意:未定义变量:第13行test.php中的重定向_uri 注意:未定义索引:第29行test.php中的access_token
curl_setopt($ch, CURLOPT_VERBOSE, true);
<?php
$client_id = "XXXXXXXXX1";
$client_secret = "XXXXXXXXXX2";
$redirect_uri = "XXXXXXXXX3"; // FIXED VARIABLE NAMING HERE
$auth_code = htmlspecialchars($_GET["code"]);
$post_field_array = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'grant_type' => 'authorization_code',
'code' => $auth_code,
'redirect_uri' => $redirect_uri,
'scope' => 'basic genomes');
$post_fields = '';
foreach ($post_field_array as $key => $value)
$post_fields .= "$key=" . urlencode($value) . '&';
$post_fields = rtrim($post_fields, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.23andme.com/token/');
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);
// DUMP RESPONSE IF ERROR OCCURS, ACCESS WON'T EXIST
var_dump($response);
$access_token = $response['access_token'];
print $access_token;