Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with Google Calendar API - 401 Login required when adding a calendar event

As I am developing an exportation and importing into Google Calendar application with Codeigniter and the Google apiService/apiCalendarService. I have everything set up perfectly, no problem with this, when I authorize my application with Google from this page:

enter image description here

The contained code is what happens when the redirect happens:

session_start();

require(APPPATH . 'libraries/google/apiClient.php');
require(APPPATH . 'libraries/google/contrib/apiCalendarService.php');

$apiClient = new apiClient();
$apiClient->setUseObjects(true);
$calendarService = new apiCalendarService($apiClient);

if(isset($_SESSION['oauth_access_token'])) 
{
    $apiClient->setAccessToken($_SESSION['oauth_access_token']);
} 
else 
{
    $token = $apiClient->authenticate();
    $_SESSION['oauth_access_token'] = $token;
}

After authenticating, I get redirected back to my localhost site with the options in my URL:

http://localhost/project/acp/sync/auth/?code=4/DS91JtSJ5_9Q-Z55Kpyh2AicyWdL

My script will detect when an authencation has been made by checking the URI segment with auth and a code query string - so I can start importing events to my Google Calendar account, until that I get this error thrown:

Fatal error: Uncaught exception 'apiServiceException' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key={removed API key}: (401) Login Required' in /Users/Me/Sites/project/application/libraries/google/io/apiREST.php:86
Stack trace:
#0 /Users/Me/Sites/project/application/libraries/google/io/apiREST.php(56): apiREST::decodeHttpResponse(Object(apiHttpRequest))
#1 /Users/Me/Sites/project/application/libraries/google/service/apiServiceResource.php(187): apiREST::execute(Object(apiServiceRequest))
#2 /Users/Me/Sites/project/application/libraries/google/contrib/apiCalendarService.php(493): apiServiceResource->__call('insert', Array)
#3 /Users/Me/Sites/project/application/controllers/acp/panel.php(2053): EventsServiceResource->insert('primary', Object(Event))
#4 [internal function]: Panel->sync('auth')
#5 /Users/Me/Sites/project/system/core/CodeIgniter.php(359): call_user_func_array(Array, Array)
#6 /Users/Me in /Users/Me/Sites/project/application/libraries/google/io/apiREST.php on line 86

The code that was importing an event (code came from Google API samples):

session_start();

require(APPPATH . 'libraries/google/apiClient.php');
require(APPPATH . 'libraries/google/contrib/apiCalendarService.php');

$apiClient = new apiClient();
$apiClient->setUseObjects(true);
$service = new apiCalendarService($apiClient);

$event = new Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new EventDateTime();
$start->setDateTime('2011-06-03T10:00:00.000-07:00');
$event->setStart($start);
$end = new EventDateTime();
$end->setDateTime('2011-06-03T10:25:00.000-07:00');
$event->setEnd($end);
$attendee1 = new EventAttendee();
$attendee1->setEmail('attendeeEmail');
// ...
$attendees = array($attendee1,
                   // ...
                  );
$event->attendees = $attendees;
$createdEvent = $service->events->insert('primary', $event);

echo $createdEvent->getId();

I tried Googling the error, but I don't seem to find any solutions with this, do you know how I can fix this problem?

like image 335
MacMac Avatar asked Oct 09 '22 12:10

MacMac


1 Answers

You are getting a 401 Login Required because the second code is not setting the token:

$apiClient->setAccessToken($_SESSION['oauth_access_token']);
like image 135
Juan Mellado Avatar answered Oct 13 '22 01:10

Juan Mellado