I'm trying to access a public calendar (from Google Calendar) that contains national holidays:
calendarId: 'pt_br.brazilian#[email protected]'
As the calendar is public, I thought I would be able to access it using only the API Key:
function OnLoadCallback() { var config = { client_id: '32j4lk32j5kj342l5h.googleuser.com', //fake client id scope: 'https://www.googleapis.com/auth/calendar.readonly' }; gapi.client.setApiKey('fId345AM20HXXXXXXXXXXXXXXXXgT3f9kyp2REfkaw2'); //fake api key gapi.client.load('calendar', 'v3', function() { var today = new Date(), request; request = gapi.client.calendar.calendarList.get({ calendarId: 'pt_br.brazilian#[email protected]', timeMin: (new Date(today.getFullYear(), today.getMonth(), today.getDay(), 0, 0, 0, 0)).toISOString(), timeMax: (new Date(today.getFullYear(), today.getMonth(), today.getDay(), 23, 59, 59, 999)).toISOString(), fields: 'items(creator(displayName,email),end,endTimeUnspecified,start,summary)' }); request.execute(function(response) { window.alert('length of items: ' + response.items.length); }); }); }
However, I keep getting the following response, which is a 401 (unauthorized) error:
{ "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Login Required" } }
Can someone clarify whether what I'm trying to do is achievable or not?
And finally if it is possible - what do I have to change referring to my current code?
The strict answer to "Is there a way to do this without OAuth and just an API key and/or client id and secret?" is no. However, you can achieve what you are looking for using OAuth. You simply need to store a Refresh Token, which you can then use any time to request an Auth Token to access your gmail.
Enable API accessIn the API Manager pane, click Library to see the list of available APIs. In the Google Apps APIs list, scroll down to click or search for Calendar API, then on the API page, click Enable.
On the left side of the screen, click the name of the calendar you want to embed. In the "Integrate calendar" section, copy the iframe code displayed. Under the embed code, click Customize. Choose your options, then copy the HTML code displayed.
I was able to do the same thing using jQuery.
var mykey = 'your_api_key'; // typically like Gtg-rtZdsreUr_fLfhgPfgff var calendarid = 'you_calendar_id'; // will look somewhat like [email protected] $.ajax({ type: 'GET', url: encodeURI('https://www.googleapis.com/calendar/v3/calendars/' + calendarid+ '/events?key=' + mykey), dataType: 'json', success: function (response) { //do whatever you want with each }, error: function (response) { //tell that an error has occurred } });
However you need to be sure you have done the following:-
1) Register a project at https://code.google.com/apis/console
2) Generate a Simple API Access key
3) Ensure Calendar API is activated under services.
Read more at https://developers.google.com/google-apps/calendar/firstapp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With