0

So I have this code and it is working properly ONLY when I am logged in w/ my google account. When I'm not logged in I have to log in in order to continue my create event function. Is there any way to make it so that anyone can add event to my public calendar? I've searched many places but I can't seem to find any answers. :P

var CLIENT_ID = 'MY CLIENT_ID GOES HERE';
var SCOPES = ["https://www.googleapis.com/auth/calendar"];

checkAuth();

function checkAuth() {
    gapi.auth.authorize(
        {
            'client_id': CLIENT_ID,
            'scope': SCOPES.join(' '),
            'immediate': true
        }, handleAuthResult);
}

function handleAuthResult(authResult) {
    if (authResult && !authResult.error) {
        console.log(authResult);
        loadCalendarApi();
    } else {
        gapi.auth.authorize(
            {client_id: CLIENT_ID, scope: SCOPES, immediate: false},
            handleAuthResult);
        return false;
    }
}

function loadCalendarApi() {
    gapi.client.load('calendar', 'v3', createEvent);
}

function createEvent(){
    var resource = {
        "summary": eventName,
        "location": location,
        "description": rid,
        "start": {
                    "dateTime": startNew
                },
        "end": {
                    "dateTime": endNew
                },
    };
    var request = gapi.client.calendar.events.insert({
        'calendarId': 'MY CALENDAR ID GOES HERE@group.calendar.google.com',
        'resource': resource
    });
    request.execute(function(resp) {
        console.log(resp);
        callback();
    });
}

Notice some of the codes where left out. But everything is working properly.

inertia
  • 3,997
  • 2
  • 17
  • 26

1 Answers1

0

You can try creating a service account:

A service account's credentials include a generated email address that is unique and at least one public/private key pair.

Your application now has the authority to make API calls as users in your domain (to "impersonate" users). When you prepare to make authorized API calls, you specify the user to impersonate.

You can follow this step from thread:

  1. You need to go to the Google Developer's console and mark your account as a 'service account'. This will differentiate it from a web application. The important difference is that nobody will be prompted to log in to their account before the events are added since the account will belong to your application, not an end user. For more information see this article, starting on page 5.
  2. You need to create a public/private key pair. From the developer's console, click on Credentials. Under you service account, click on 'Generate new P12 key'. You'll need to store this somewhere. That file location becomes the $key_file_location variable string in the code below.
  3. Also from the developer's console, you need to enable the Calendar API.
  4. In Google Calendar that you want to add events to, under settings, click Calendar Settings then on 'Share this Calendar' at the top. Under 'Share with specific people' in the 'Person' field, paste in the email address from the service account credentials. Change the permission settings to 'Make changes to events'. Don't forget to save the change.

There are also some links in the thread that would help you on implementing service account to your website.

I hope this helps.

Community
  • 1
  • 1
Mr.Rebot
  • 6,703
  • 2
  • 16
  • 91
  • 1
    Also the service account will need to have write access to this calendar. That means one needs to change the ACLs of the target calendar to contain the email address of the service account. There are a few complications if the target calendar is domain-owned as domain maximum ACLs might apply. In that case domain-wide delegation of authority might be required. – luc May 30 '16 at 08:08
  • So i guess you can achieve this with node.js but is there any way for me to accomplish this with javascript only? – inertia May 31 '16 at 16:24