Created
May 11, 2012 02:26
-
-
Save AstDerek/2657113 to your computer and use it in GitHub Desktop.
Some Oauth
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 | |
class Google_OAuth { | |
var $client_id; | |
var $client_secret; | |
var $redirect_uri; | |
var $scopes = array( | |
'userinfo' => array( | |
'scope' => 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email', | |
'endpoint' => 'https://www.googleapis.com/oauth2/v2/userinfo', | |
) | |
); | |
var $scope = 'userinfo'; | |
var $auth_url = 'https://accounts.google.com/o/oauth2/auth'; | |
var $token_url = 'https://accounts.google.com/o/oauth2/token'; | |
function __construct ($client_id='', $client_secret='', $redirect_uri='', $scope='') { | |
$this->client_id = $client_id; | |
$this->client_secret = $client_secret; | |
$this->redirect_uri = $redirect_uri; | |
$this->scope = $scope; | |
} | |
function request ($url='',$type='GET',$data=NULL,$headers=array()) { | |
$type = $type == 'POST' ? 'POST' : 'GET'; | |
$ch = curl_init($url); | |
curl_setopt_array($ch,array( | |
CURLOPT_MAXREDIRS => 0, | |
CURLOPT_HEADER => TRUE, | |
CURLOPT_RETURNTRANSFER => TRUE, | |
CURLINFO_HEADER_OUT => TRUE, | |
)); | |
if ($type == 'POST') { | |
curl_setopt_array($ch,array( | |
CURLOPT_POST => TRUE, | |
CURLOPT_POSTFIELDS => is_array($data) ? http_build_query($data) : '' | |
)); | |
} | |
else if (is_array($data) && count($data)) { | |
$url .= '?' . http_build_query($data); | |
} | |
curl_setopt_array($ch,array( | |
CURLOPT_URL => $url, | |
CURLOPT_HTTPHEADER => $headers | |
)); | |
$response = curl_exec($ch); | |
$headers_sent = curl_getinfo($ch,CURLINFO_HEADER_OUT); | |
curl_close($ch); | |
if (!strlen($response)) { | |
$response = '{error:"Empty response from server"}'; | |
$headers = 'Empty'; | |
} | |
else { | |
list($headers,$response) = explode("\r\n\r\n",$response,2); | |
if (!strlen($response)) { | |
$response = '{error:"Empty response from server"}'; | |
} | |
} | |
$return = json_decode($response); | |
$return->request_headers = $headers_sent; | |
$return->response_headers = $headers; | |
$return->original_response = $response; | |
return $return; | |
} | |
function request_access_code () { | |
$fields = array( | |
'response_type' => 'code', | |
'scope' => $this->scopes[$this->scope]['scope'], | |
'client_id' => $this->client_id, | |
'redirect_uri' => $this->redirect_uri, | |
'access_type' => 'online' //'offline' | |
); | |
$url = $this->auth_url . '?' . http_build_query($fields); | |
header('Location: ' . $url); | |
return htmlspecialchars($url); | |
} | |
function request_access_token ($code) { | |
$fields = array( | |
'code' => $code, | |
'client_id' => $this->client_id, | |
'client_secret' => $this->client_secret, | |
'redirect_uri' => $this->redirect_uri, | |
'scope' => '',//$this->scopes[$this->scope], | |
'grant_type' => 'authorization_code' | |
); | |
$return = $this->request($this->token_url,'POST',$fields); | |
$return->method = 'request_access_token'; | |
return $return; | |
} | |
function request_scope ($token) { | |
$return = $this->request($this->scopes[$this->scope]['endpoint'],'GET',NULL,array('Authorization: OAuth ' . urlencode($token))); | |
$return->method = 'request_scope'; | |
return $return; | |
} | |
function consume_code ($code) { | |
$response = $this->request_access_token($code); | |
if (isset($response->access_token)) { | |
return $this->request_scope($response->access_token); | |
} | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment