Created
May 26, 2015 10:28
-
-
Save dtbaker/8b7898780e2dfbb993fc to your computer and use it in GitHub Desktop.
test envato api oauth for UBLThemes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| ini_set('display_errors',true); | |
| ini_set('error_reporting',E_ALL);; | |
| $clientid = 'XXXXXXXXXXXXXXXXXXX'; | |
| $fields_string = ''; | |
| $errors = array(); | |
| if ( isset( $_GET['code'] ) ) { | |
| $clientsecret = 'XXXXXXXXXXXXXXXXXXX'; | |
| $returnurl = 'http://dtbaker.net/files/envato/test_api.php'; | |
| $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 ) { | |
| print_r($initialdata); | |
| //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 ); | |
| echo $cinit_username_data; | |
| $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 | |
| echo "Hello $username"; | |
| } | |
| } | |
| print_r($errors); | |
| }else{ | |
| ?> | |
| <a href="https://api.envato.com/authorization?response_type=code&client_id=<?php echo $clientid;?>&redirect_uri=http://dtbaker.net/files/envato/test_api.php">Login to Envato</a> | |
| <?php | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment