Created
November 4, 2013 20:14
-
-
Save pabloroman/7308541 to your computer and use it in GitHub Desktop.
Disqus callback
This file contains 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
public function action_disqus() { | |
$input = Input::all(); | |
if ( isset( $input['error'] ) ) { | |
die( $input['error'] ); | |
} | |
if( isset( $input['verify'] ) ) { | |
$code = $input['code']; // A temporary token which you will exchange for a finalized access token. | |
$username = $input['username']; // The username of the user who you're requesting authorization of. | |
$userid = $input['user_id']; // A unique identifier for this user which is guaranteed not to change. | |
$audiencesync_uri = urlencode($input['audiencesync_uri']); // The URL to which you will redirect the user once you've confirmed their authorization. | |
$next_url = URL::to( "auth/disqus?user_id=".$userid."&success=1&code=".$code."&audiencesync_uri=".$audiencesync_uri ); | |
return View::make( 'auth.disqus_verify', array( 'next_url' => $next_url ) ); | |
} else { | |
// Get the code for request access | |
$code = $input['code']; | |
$audiencesync_uri = urldecode($input['audiencesync_uri']); | |
// Request the access token | |
$disqus_key = Config::get('disqus.secret_key'); | |
$disqus_public = Config::get('disqus.public_key'); | |
$url = 'https://disqus.com/api/oauth/2.0/access_token/'; | |
$fields = array( | |
'grant_type' => urlencode( "audiencesync" ), | |
'code' => urlencode( $code ), | |
'client_id' => urlencode( $disqus_public ), | |
'client_secret' => urlencode( $disqus_key ), | |
'redirect_uri' => URL::to( 'auth/disqus?verify=1' ) | |
); | |
$fields_string = http_build_query($fields, NULL, '&'); | |
//open connection | |
$ch = curl_init(); | |
//set the url, number of POST vars, POST data | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt( $ch, CURLOPT_URL, $url ); | |
curl_setopt( $ch, CURLOPT_POST, count( $fields ) ); | |
curl_setopt( $ch, CURLOPT_POSTFIELDS, $fields_string ); | |
//execute post | |
$result = curl_exec($ch); | |
//close connection | |
curl_close($ch); | |
$auth_results = json_decode( $result ); | |
if( isset( $auth_results->error ) ) { | |
var_dump( $auth_results ); | |
exit; | |
} | |
// Extract access token and render | |
$access_token = $auth_results->access_token; | |
$user_id = $auth_results->user_id; | |
$success = $input['success']; | |
$completion_url = $audiencesync_uri."?client_id=".$disqus_public."&user_id=".$user_id."&access_token=".$access_token."&success=".$success; | |
return View::make( 'auth.disqus_success', array( 'redirect_url' => $redirect_url ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment