Created
January 10, 2012 12:48
-
-
Save alexbilbie/1588897 to your computer and use it in GitHub Desktop.
Access token grab
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
<?php | |
function signin() | |
{ | |
if ($code = $_GET['code']) | |
{ | |
$params = array( | |
'client_id' => $this->client_id, | |
'client_secret' => $this->client_secret, | |
'redirect_uri' => $this->redirect_uri, | |
'grant_type' => 'authorization_code', | |
'code' => $code | |
); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'https://sso.lincoln.ac.uk/oauth/access_token'); | |
curl_setopt($ch, CURLOPT_POST, count($params)); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$result = curl_exec($ch); | |
if ($result) | |
{ | |
$json = json_decode($result); | |
if ($json->error == 1) | |
{ | |
echo 'SSO error - ' . $json->error_message; | |
} | |
else | |
{ | |
$access_token = $json->access_token; | |
//$user_details = file_get_contents('https://nucleus-proxy.online.lincoln.ac.uk/v1/people/user?access_token=' . $access_token); | |
$user_details = file_get_contents('https://nucleus.lincoln.ac.uk/v1/people/user?access_token=' . $access_token); | |
if ($user_details) | |
{ | |
$user = json_decode($user_details); | |
if ($user->error) | |
{ | |
echo 'Nucleus error - ' . $user->message; | |
} | |
else | |
{ | |
$this->session->set_userdata(array( | |
'user_id' => $user->results[0]->id, | |
'user_name' => $user->results[0]->name | |
)); | |
// Do whatever now | |
} | |
} | |
else | |
{ | |
echo 'Failed to get user details'; | |
} | |
} | |
} | |
else | |
{ | |
echo 'Curl error - ' . curl_error($ch); | |
} | |
curl_close($ch); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment