murugan-project / google-api-php-client

Automatically exported from code.google.com/p/google-api-php-client
Apache License 2.0
0 stars 0 forks source link

Google Calendar not retrieving any event #376

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.$client = new Google_Client();
2.$cal = new Google_CalendarService($client);
3.$events = $cal->events->listEvents('email address');

What is the expected output? What do you see instead?

I am supposed to get the list of all events on my google calendar in the form 
of an array. Instead, I end up with an empty array the contains no events from 
my google calendar.

What version of the product are you using? On what operating system?
I am using php 5.3 and I am working on windows 8.

Please provide any additional information below.

Below is my code:
<?php

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';

session_start();

$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");

// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('1025065880500-82g2qf4dj0m6tc72tfaem2d34psepl9u.apps.google
usercontent.com');
$client->setClientSecret('bluCB0ptIq7-DV0Dt92eBIaM');
$client->setRedirectUri('redirect uri');
$client->setDeveloperKey('AIzaSyBY4jVeOjDEFYMv0AJynCZrMw61vBg7YVE');

$cal = new Google_CalendarService($client);

$events = $cal->events->listEvents('email address');

echo $events;

?>

All that is printed out is an empty Array. I also tried to add an event to the 
calendar to see if it was possible but all I end up with is an error that says:

Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error 
calling POST https://www.googleapis.com/calendar/v3/calendars/'email address 
for google 
calendar'/events?key='key'......................................lib/google-api-p
hp-client/src/io/Google_REST.php on line 66

Original issue reported on code.google.com by kenshine...@gmail.com on 23 Aug 2013 at 10:45

GoogleCodeExporter commented 9 years ago
I'm having a very similar issue, and I'm trying to use a Service Account to 
interact with a Google Apps calendar.

Original comment by j...@boddiford.com on 5 Sep 2013 at 6:11

GoogleCodeExporter commented 9 years ago
Here is my code and the error it produces.

require_once '../assets/google-api-php-client/src/Google_Client.php';
require_once 
'../assets/google-api-php-client/src/contrib/Google_CalendarService.php';

define('SERVICE_ACCOUNT_NAME', 'XXXXXXX@developer.gserviceaccount.com');
define('CLIENT_ID','XXXXXXX.apps.googleusercontent.com');
define('KEY_FILE', 'XXXXXXXXX2123e906b6340d2ccba173225c55-privatekey.p12');
define('CALENDAR_ID','USER@GOOGLEAPPSFORBUSINESSDOMAIN.com');

$client = new Google_Client();
$client->setApplicationName("My Application Name");
$client->setClientId(CLIENT_ID);

/* Note: make sure to call $client->setUseObjects(true) if you want to see
 * objects returned instead of data (this example code uses objects)
 */
$client->setUseObjects(true);

/*session_start();
if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
}
*/
/* Load the key in PKCS 12 format - remember: this is the file you had to
 * download when you created the Service account on the API console.
 */
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/calendar'),
    $key)
);

if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
}

/* ------------------------- We are now properly authenticated 
------------------- */

$cal = new Google_CalendarService($client);

date_default_timezone_set('America/New_York');
$start = date(DATE_ATOM,strtotime('now'));
$end = date(DATE_ATOM,strtotime('+1 month'));

$params = array(
    'singleEvents' => 'true',
    'timeMax' => $end,
    'timeMin' => $start,
    'orderBy' => 'startTime');

$event_list = $cal->events->listEvents(CALENDAR_ID, $params);

$events = $event_list->getItems();
echo '<table border="1"><thead>';
echo '<th>ID</th><th>SAFE</th><th>Start</th><th>End</th></tr></thead>';
foreach ($events as $event) {
echo '<tr>';
echo '<td>'.$event->getId().'</td>';
echo '<td>'.$event->getSummary().'</td>';
echo '<td>'.$event->getStart()->getDateTime().'</td>';
echo '<td>'.$event->getEnd()->getDateTime().'</td>';
echo '</tr>';
}
echo '</table>';

ERROR PRODUCED:

Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error 
calling GET 
https://www.googleapis.com/calendar/v3/calendars/USER@GOOGLEAPPSFORBUSINESSDOMAI
N.com/events?singleEvents=true&timeMax=2013-10-05T01%3A57%3A49-04%3A00&timeMin=2
013-09-05T01%3A57%3A49-04%3A00&orderBy=startTime&key=AIzaSyC0cnTbebaBQ9e92FcbMr2
YZt0x4Aqzxq4: (404) Not Found' in 
/Users/jboddiford/Production/LeaveRequest/website/assets/google-api-php-client/s
rc/io/Google_REST.php:66 Stack trace: #0 
/Users/jboddiford/Production/LeaveRequest/website/assets/google-api-php-client/s
rc/io/Google_REST.php(36): 
Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 
/Users/jboddiford/Production/LeaveRequest/website/assets/google-api-php-client/s
rc/service/Google_ServiceResource.php(186): 
Google_REST::execute(Object(Google_HttpRequest)) #2 
/Users/jboddiford/Production/LeaveRequest/website/assets/google-api-php-client/s
rc/contrib/Google_CalendarService.php(529): 
Google_ServiceResource->__call('list', Array) #3 
/Users/jboddiford/Production/LeaveRequest/websit in 
/Users/jboddiford/Production/LeaveRequest/website/assets/google-api-php-client/s
rc/io/Google_REST.php on line 66

Original comment by j...@boddiford.com on 5 Sep 2013 at 6:14

GoogleCodeExporter commented 9 years ago
Have you enable the Calendar API in the API console? And ensured that service 
accounthas the proper scopes? It may be easier to ask the Calendar team 
directly about this one - as on a PHP level the code looks OK. 

Original comment by ianbar...@google.com on 5 Sep 2013 at 12:20

GoogleCodeExporter commented 9 years ago
Hi, Yes... I have verified the Calendar API is enabled in the console and I 
have ensured the service account has proper access scope in Google Apps.  I 
would LOVE to contact the Calendar team directly... can you provide their 
contact information? 

I've been posting in Google Forums about this and have had no luck with a 
response from anyone at Google.

Thanks so much!
Josh

Original comment by j...@boddiford.com on 5 Sep 2013 at 12:37

GoogleCodeExporter commented 9 years ago
Usually stack overflow is the best place to go. Couple of things to try: 

Try sharing the calendar (via the UI) with the service account email address, 
see if that helps. 
Or
Try setting the sub param on the assertion credentials, so you get a token for 
that user, e.g.
$cred = new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/calendar'),
    $key);
$cred->sub = "user@appsforbusinessdomain.com"; // whatever this should be.
$client->setAssertionCredentials($cred);

and then try exchanging for a token, and seeing if you have any more joy that 
way. 

Original comment by ianbar...@google.com on 5 Sep 2013 at 1:02

GoogleCodeExporter commented 9 years ago
I was able to gain access to the calendar, and list the contents, by adding the 
service account email address as a MANAGER of the calendar.  So that at least 
worked... BUT having to do that defeats the whole purpose of having the service 
account as that would require each user to edit their calendar security 
settings so my app could insert onto them... does that sound right?

I will try out the second suggestion you have here regarding $cred and let you 
know.

Is there a specific area on Stack Exchange I should be submitting?

Thanks for the help!
Josh

Original comment by j...@boddiford.com on 5 Sep 2013 at 1:11

GoogleCodeExporter commented 9 years ago
OK,

So I tried the second suggestion you provided and I'm getting a different error 
now.  It's an "Access Denied" error which sounds like we're getting closer! :-)

Fatal error: Uncaught exception 'Google_AuthException' with message 'Error 
refreshing the OAuth2 token, message: '{ "error" : "access_denied" }'' in 
/Users/jboddiford/Production/LeaveRequest/website/assets/google-api-php-client/s
rc/auth/Google_OAuth2.php:288 Stack trace: #0 
/Users/jboddiford/Production/LeaveRequest/website/assets/google-api-php-client/s
rc/auth/Google_OAuth2.php(265): Google_OAuth2->refreshTokenRequest(Array) #1 
/Users/jboddiford/Production/LeaveRequest/website/assets/google-api-php-client/s
rc/auth/Google_OAuth2.php(218): Google_OAuth2->refreshTokenWithAssertion() #2 
/Users/jboddiford/Production/LeaveRequest/website/assets/google-api-php-client/s
rc/service/Google_ServiceResource.php(167): 
Google_OAuth2->sign(Object(Google_HttpRequest)) #3 
/Users/jboddiford/Production/LeaveRequest/website/assets/google-api-php-client/s
rc/contrib/Google_CalendarService.php(529): 
Google_ServiceResource->__call('list', Array) #4 
/Users/jboddiford/Production/LeaveRequest/website/test/calendar2.php(110): 
Google_EventsServiceRe in 
/Users/jboddiford/Production/LeaveRequest/website/assets/google-api-php-client/s
rc/auth/Google_OAuth2.php on line 288

Original comment by j...@boddiford.com on 5 Sep 2013 at 1:26

GoogleCodeExporter commented 9 years ago
Did You found the solutions?

Original comment by tarmo1...@gmail.com on 9 Oct 2013 at 5:37

GoogleCodeExporter commented 9 years ago
This issue tracker is now closing. Development on the Google PHP client library 
moved to GitHub with the release of the 1.0.0-alpha, and now the 1.0 branch has 
reached beta status there will be no further releases of the 0.6 branch of the 
library. 

Please take a look at the latest version on 
https://github.com/google/google-api-php-client

For information on migrating, please take a look at this guide: 
https://developers.google.com/api-client-library/php/guide/migration

For general library support please ask a question on StackOverflow: 
http://stackoverflow.com/questions/tagged/google-api-php-client

If you are looking for support with a specific API, please contact the team 
working with that API via StackOverflow or their preferred support mechanism. 

If your issue still exists with the new version of the library, please raise a 
bug in the GitHub issue tracker with a minimal code sample. 

Thanks!

Original comment by ianbar...@google.com on 22 Jan 2014 at 4:53

GoogleCodeExporter commented 9 years ago
All that is printed out is an empty Array. I also tried to add an event to the 
calendar to see if it was possible but all I end up with is an error that says:

Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error 
calling POST https://www.googleapis.com/calendar/v3/calendars/'email address 
for google 
calendar'/events?key='key'......................................lib/google-api-p
hp-client/src/io/Google_REST.php on line 66

Original comment by develo...@skyzon.com on 13 Mar 2015 at 4:53