I have this medium API Request Data. Medium Docs
Example request:
GET /v1/me HTTP/1.1
Host: api.medium.com
Authorization: Bearer 181d415f34379af07b2c11d144dfbe35d
Content-Type: application/json
Accept: application/json
Accept-Charset: utf-8
In php CODE SAMPLE below, Everything is working fine as I can get my data successfully from Medium API
$url ="https://api.medium.com/v1/me";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Authorization: Bearer 29f812842-xxxxxxxxxxx'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo $output = curl_exec($ch);
Here is my Issue. When I tried to fetch the data from javascript it throw error below
XHROPTIONS
https://api.medium.com/v1/me
CORS Missing Allow Origin
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.medium.com/v1/me. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 401.
below is the code causing the error
async function GetUsersData() {
try {
const response = await fetch('https://api.medium.com/v1/me', {
method: 'GET',
mode: 'cors',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer 29f8128xxxx'
}
});
console.log(response);
alert(response);
} catch (error) {
// handle the error
alert(error);
}
}
GetUsersData();