Reputation: 18379
I'm looking for a very simple example of using the Google Calendar API using HTTP GET or POST
All there examples require these huge libraries for language X. I just want a raw http example that would work in any language and require no libraries.
i.e. https://www.googleapis.com/calendar/v3/users/me/calendarList/primary?key=mykey
But of course this does not work, I don't think there is a key option for your Google API key, and you need to authorize it somehow.
A raw example in Java or JavaScript would be ideal,
Something like,
HttpPost request = new HttpPost("https://www.googleapis.com/calendar/v3/users/me/calendarList/primary?key=mykey");
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY),
new UsernamePasswordCredentials(user, password));
HttpResponse response = client.execute(request);
But.. that works, and what are user/password or how to validate Auth...
Any help is vastly appreciated.
Upvotes: 8
Views: 2796
Reputation: 1934
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]
Upvotes: 1
Reputation: 18379
This question was answered in this post.
The answer post references a how2 that shows you how to call any google auth API in three simple steps using just HTTP GET/POST and not requiring any client libraries.
I spent over a day trying to get something working using Google's how2s and client libraries, and did not end up with anything I could use. But following this how2, I got it working in under and hour in my app. Thanks a lot to the blogger.
call Google auth API using Apache HttpClient
Upvotes: 5
Reputation: 6791
You might want to check the JavaScript Quickstart or Java Quickstart for a simple sample. Upon checking the Authorizing Requests to the Google Calendar API document, it stated that:
Every request your application sends to the Google Calendar API must include an authorization token. The token also identifies your application to Google.
Your application must use OAuth 2.0 to authorize requests. No other authorization protocols are supported. If your application uses Google Sign-In, some aspects of authorization are handled for you.
Also, if you want to have a very concrete sample of Google Calendar API using HTTP GET or POST, you can use the Try it! which you can see in each API Reference. Note that, there is an authorize and execute button (OAuth 2.0).
Tip: The Google APIs client libraries can handle some of the authorization process for you. They are available for a variety of programming languages; check the page with libraries and samples for more details.
Hope this helps!
Upvotes: 2