When i try to submit a new event to my google calender in a laravel project I always face this error:

Google_Service_Exception (401) { "error": "unauthorized_client", "error_description": "Unauthorized" }

I created new OAuth credentials for calendar api and added it to .env file as shown:

.env

I am also using google+ api so that each user can access his calender to update his events via OAuth 2

I tried to add new OAuth credentials but the problem still persist I also tried to delegate domain-wide authority to my service account by adding client id added to the .env file & scope and authorize them but nothing changed I also waited for 24 hours waiting the changes to take place but also nothing changed

Here is the function am using to create new events:

public function doCreateEvent(Event $evt, Request $request)
{
    $this->validate($request, [
        'title' => 'required',
        'calendar_id' => 'required',
        'datetime_start' => 'required|date',
        'datetime_end' => 'required|date'
    ]);

    $title = $request->input('title');
    $calendar_id = $request->input('calendar_id');
    $start = $request->input('datetime_start');
    $end = $request->input('datetime_end');

    $start_datetime = Carbon::createFromFormat('Y/m/d H:i', $start);
    $end_datetime = Carbon::createFromFormat('Y/m/d H:i', $end);

    $cal = new \Google_Service_Calendar($this->client);
    $event = new \Google_Service_Calendar_Event();
    $event->setSummary($title);

    $start = new \Google_Service_Calendar_EventDateTime();
    $start->setDateTime($start_datetime->toAtomString());
    $event->setStart($start);
    $end = new \Google_Service_Calendar_EventDateTime();
    $end->setDateTime($end_datetime->toAtomString());
    $event->setEnd($end);

    // Create new conference
    $conference = new \Google_Service_Calendar_ConferenceData();

    $entryPoint = new \Google_Service_Calendar_EntryPoint();
    $entryPoint->setAccessCode('wx12z3s');
    $entryPoint->setEntryPointType('video');
    $entryPoint->setLabel('meet.google.com/wx12z3s');
    $entryPoint->setMeetingCode('wx12z3s');
    $entryPoint->setPasscode('wx12z3s');
    $entryPoint->setPassword('wx12z3s');
    $entryPoint->setPin('wx12z3s');
    $entryPoint->setUri('https://meet.google.com/wx12z3s');

    $conference->setEntryPoints($entryPoint);

    $conferenceSolution = new \Google_Service_Calendar_ConferenceSolution();
    $conferenceSolution->setIconUri(null);
    $conferenceSolution->setKey(new \Google_Service_Calendar_ConferenceSolutionKey());

    $conference->setConferenceSolution($conferenceSolution);

    $conferenceRequest = new \Google_Service_Calendar_CreateConferenceRequest();
    $conferenceRequest->setRequestId($request->_token);
    $conferenceSolutionKey = new \Google_Service_Calendar_ConferenceSolutionKey();

    $conferenceSolutionKey->setType("hangoutsMeet");
    $conferenceRequest->setConferenceSolutionKey($conferenceSolutionKey);
    $conferenceRequest->setStatus(new \Google_Service_Calendar_ConferenceRequestStatus());

    $conference->setCreateRequest($conferenceRequest);

    $event->setConferenceData($conference);

    //attendee
    if ($request->has('attendee_name')) {
        $attendees = [];
        $attendee_names = $request->input('attendee_name');
        $attendee_emails = $request->input('attendee_email');

        foreach ($attendee_names as $index => $attendee_name) {
            $attendee_email = $attendee_emails[$index];
            if (!empty($attendee_name) && !empty($attendee_email)) {
                $attendee = new \Google_Service_Calendar_EventAttendee();
                $attendee->setEmail($attendee_email);
                $attendee->setDisplayName($attendee_name);
                $attendees[] = $attendee;
            }
        }

        $event->attendees = $attendees;
    }

    $created_event = $cal->events->insert($calendar_id, $event);

    $evt->title = $title;
    $evt->calendar_id = $calendar_id;
    $evt->event_id = $created_event->id;
    $evt->datetime_start = $start_datetime->toDateTimeString();
    $evt->datetime_end = $end_datetime->toDateTimeString();
    $evt->save();

    return redirect('/event/create')
                ->with('message', [
                    'type' => 'success',
                    'text' => 'Event was created!'
                ]);
}

I am using a G suite account so that I can add events and assign hangout meet conferences to it but the problem keeps showing when i try to add newly created event to the user calender

1

There are 1 best solutions below

0
Kareem Saeed On BEST ANSWER

The problem was the access token I am trying to use to access user calendar is wrong. When I deleted all user data and of course his access token saved to the database and tried to login again so that a new record created to the user with a new access token the problem solved and I can now access his calendar and create new events