Seriously feeling your pain right now. This should not be that difficult, but it would seem that Google REALLY doesn't want us doing that: taken by the fact that nearly everything they offer shoves you in the direction of those client libraries.
That said, I was able to accomplish this for the OAuth2 part (sharing the code for that below), and another thing or two.
Reading from the API (GET) is one thing, but the parameters for something like creating a calendar event are so numerous that there's too many ways to error, if you're just guessing at the syntax (which is all you can do if they don't provide documentation for it). I've resigned to using the client library for everything beyond the following:
For the callback/redirect page:
$grant_type = 'authorization_code';
$url = 'https://oauth2.googleapis.com/token';
$data = array('code' => $code, 'client_id' => $client_id, 'client_secret' =>
$client_secret, 'redirect_uri' => $redirect_uri, 'grant_type' => $grant_type, 'scope' => $scope );
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$strjson = file_get_contents($url, false, $context);
if ($strjson === FALSE) { echo 'Failed to get contents. '; }
else {
$jsonobject = json_decode($strjson);
$access_token=$jsonobject->access_token;
$expires_in=$jsonobject->expires_in;
$expires = $datetime + $expires_in;
$token_type=$jsonobject->token_type;
$refresh_token=$jsonobject->refresh_token;
Refreshing a token:
$grant_type = 'refresh_token';
$url = 'https://oauth2.googleapis.com/token';
$data = array('refresh_token' => $refresh_token, 'client_id' => $client_id, 'client_secret' => $client_secret, 'redirect_uri' => $redirect_uri, 'grant_type' => $grant_type, 'scope' => $scope );
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$strjson = file_get_contents($url, false, $context);
if ($strjson === FALSE) { echo 'Failed to get contents. '; }
else
{
$jsonobject = json_decode($strjson);
$access_token=$jsonobject->access_token;
$expires_in=$jsonobject->expires_in;
$expires = $datetime + $expires_in;
$token_type=$jsonobject->token_type;
Getting calendar events:
https://www.googleapis.com/calendar/v3/calendars/[yourCalenderID]/events?access_token=[what you got from above]