Udemy has changed their API from 1.1 to 2 and added a step for authentication that has basically shut down my site pulling data using the 1.1 api. I found the example below and got it to work for the last several months but with the changes and now Udemy states that after April 20 they will not longer support 1.1 I need to get this fixed.
function get_data($url) {
$ch = curl_init();
$timeout = 5;
$udemy_client_id = 'xxxxx';
$udemy_client_secret = 'xxxxx';
HTTP Authorization
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Udemy-Client-Id: '.$udemy_client_id,
'X-Udemy-Client-Secret: '.$udemy_client_secret
));
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data('https://www.udemy.com/api-1.1/courses/'.$id.'?fields[course]=@min,tagTitles,description,-images');
echo $returned_content;
The 1.1 only asked for the client_id and client_secret values but has now added on that there must be a base64 encoded HTTP Authorization. Udemy example:
curl --user {YOUR_CLIENT_ID}:{YOUR_CLIENT_SECRET} https://www.udemy.com/api-2.0/courses
curl -H "Authorization: Basic {BASE64_ENCODED_CLIENT_ID:CLIENT_SECRET}" https://www.udemy.com/api-2.0/courses
Can someone please show me how to include the new base64 encoded HTTP Authorization into my code above or do I need to get new code written?
Hey I have solved my problem already. Udemy uses 2 diffent client_id. 1 is for the api 2.0 and another for accessing the content. My script loaded 2 different client_id and the one I used was for accessing the content, which in turn wasn't correct. I have to use the first one in order for the api to work (client_id for api 2).
Yes, you have to convert client_id and client_secret in base64. My code is in python. Not sure how you do it in php though.