2020年12月19日星期六

23andMe API authentication via PHP

According to the 23andMe API authentication guidelines (https://api.23andme.com/docs/authentication/) I'm using this script to successfully get to the 23andMe user permission page using a custom URL:

<?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';  $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");  ?>  

But when using the following script for redirect URL Im getting a 403 error:

<?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;    ?>  

Appreciate any suggestions for troubleshooting

https://stackoverflow.com/questions/65376978/23andme-api-authentication-via-php December 20, 2020 at 12:42PM

没有评论:

发表评论