Skip to content

Instantly share code, notes, and snippets.

Created May 26, 2015 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/16182e852016755f91d5 to your computer and use it in GitHub Desktop.
Save anonymous/16182e852016755f91d5 to your computer and use it in GitHub Desktop.
if(isset($_GET['code'])){
$clientid = 'XXXX';
$clientsecret = 'XXXXXXXXXXX';
$returnurl = 'XXXXXXX';
$initialurl = 'https://api.envato.com/token';
$fields = array(
'grant_type' => urlencode('authorization_code'),
'code' => urlencode($_GET['code']),
'client_id' => urlencode($clientid),
'client_secret' => urlencode($clientsecret)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string, '&');
$ch = curl_init($initialurl);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_USERAGENT, $clientid);
$cinit_data = curl_exec($ch);
$initialdata = json_decode($cinit_data, true);
curl_close($ch);
if(empty($initialdata['access_token'])){ $errors[] = 'Could not get bearer, please contact admin'; }
//now lets get the username from envato
if(!$errors){
//setting the header for the rest of the api
$bearer = 'bearer ' . $initialdata['access_token'];
$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: ' . $bearer;
$usernameurl = 'https://api.envato.com:443/v1/market/private/user/username.json';
$ch_username = curl_init($usernameurl);
curl_setopt($ch_username, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch_username, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch_username, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_username, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch_username, CURLOPT_USERAGENT, $clientid);
$cinit_username_data = curl_exec($ch_username);
$username_data = json_decode($cinit_username_data, true);
curl_close($ch_username);
if(empty($username_data['username'])){ $errors[] = 'Could not get username, please contact admin'; } else { $username = $username_data['username']; }
//do what you wish with the username
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment