google calendar api - nodejs - Error PEM routines: get_name

Viewed 138

I am trying to use the google calendar api, using node.js. I have been following a tutorial but am getting the error: error:0909006C:PEM routines:get_name:no start line

I have a .env file to store my credentials and calendar ID (Ive set up a service account in google cloud platform and allowed it access to one of my calendars)

I have installed via npm, packages googleapis (88.2.0) and dotenv (10.0.0).

Here is the the node.js code in which I am running to try to 'insert' and event into the calendar:

const {google} = require('googleapis');
require('dotenv').config();

//Provide the required configurations
const CREDENTIALS = JSON.parse(process.env.CREDENTIALS);
const calendarId = process.env.CALENDAR_ID;

//Google calendar API settings
const SCOPES = ['https://www.googleapis.com/auth/calendar'];
const calendar = new google.calendar({version : "v3"});

const auth = new google.auth.JWT(CREDENTIALS.client_email,null,CREDENTIALS.private_key_id, SCOPES);


//Get date-time string for calendar
const TIMEOFFSET = '+01:00'
const dateTimeForCalendar = () => {
    let year = "2021";
    let month = "10";
    let day = "11";
    let hour = "10";
    let minute = "00";
    let newDateTime = `${year}-${month}-${day}T${hour}:${minute}:00.000${TIMEOFFSET}`;
    let event = new Date(Date.parse(newDateTime));
    let startDate = event;
    let endDate = new Date(new Date(startDate).setHours(startDate.getHours()+3));
    return {
        'start' : startDate,
        'end': endDate
    };
}


//Insert new event to Google Calemdar
const insertEvent = async (event) => {
    try {
        let response = await calendar.events.insert({
            auth: auth,
            calendarId: calendarId,
            resource: event
        });        
    }
    catch(err) {
        console.log("Error calling insertEvent, sys error: " + err)
    }
}

let dateTime = dateTimeForCalendar();

//Event for Google Calendar
let event = {
    'summary': 'my summary',
    'description': 'My description goes here',
    'start': {
        'dateTime': dateTime['start'],
        'timeZone': 'United Kingdom'
    },
    'end': {
        'dateTime': dateTime['end'],
        'timeZone': 'United Kingdom'
    }
};

insertEvent(event).then((res)=>{
    console.log(res);}).catch((err)=>{
        console.log(err)
    })

When I run the file, I get the error: error:0909006C:PEM routines:get_name:no start line

I have checked that the details in my .env file are correctly retrieved from this js page.

Is anyone able to help point me in the right direction as to what I am doing wrong?

Thanks!

1 Answers

After some playing around I managed to get this working in the end with a few changes.

  1. Created a new google cloud project and service account from scratch (in the first one I had played around with oAuth settings and not sure if that cause a problem)
  2. Changed the code CREDENTIALS.private_key_id to CREDENTIALS.private_key
  3. Removed the timeZone sub-property from start and end properties as they were not recognised (need to find out what it should be, but for now it worked with it being left out)
Related