OAuth2 Redirect URI: What do I put in it? (PHP)

6,864 views
Skip to first unread message

JS

unread,
Jan 7, 2012, 8:42:19 PM1/7/12
to google-ca...@googlegroups.com
I'm trying to use OAuth 2.0 to give my app access to users' private calendars. From what I can tell, when the user hits submit on the OAuth dialog, the page is redirected to the redirect URI. I've been looking all over, but I can't figure out what needs to go in the index of the redirect URI in order to go back and continue executing the app's code with authorization. I'm using PHP. Please ask questions if I'm being unclear. All help is appreciated!

Dan Holevoet

unread,
Jan 9, 2012, 1:20:43 PM1/9/12
to google-ca...@googlegroups.com
Hi,

Can I assume you're using v3 of the API? The redirect URI is a page you control, your PHP code. This can be a redirect back to the page the user left from, or a different page, depending on how you want to write your code.

If you're trying to do it in one page, this example should help you, though it's for Tasks instead of Calendar: http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/tasks/index.php.

Thanks,
Dan

On Sat, Jan 7, 2012 at 5:42 PM, JS <js.ste...@gmail.com> wrote:
I'm trying to use OAuth 2.0 to give my app access to users' private calendars. From what I can tell, when the user hits submit on the OAuth dialog, the page is redirected to the redirect URI. I've been looking all over, but I can't figure out what needs to go in the index of the redirect URI in order to go back and continue executing the app's code with authorization. I'm using PHP. Please ask questions if I'm being unclear. All help is appreciated!

--
You received this message because you are subscribed to the Google Groups "Google Calendar API" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-calendar-api/-/dWqx__OnhV0J.
To post to this group, send email to google-ca...@googlegroups.com.
To unsubscribe from this group, send email to google-calendar...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-calendar-api?hl=en.



--
Dan Holevoet
Google Developer Relations

JS

unread,
Jan 9, 2012, 9:29:03 PM1/9/12
to google-ca...@googlegroups.com
Okay, so I made my redirect URI bring me to a file ("home.php") in my root directory. I'm trying to just get something to work, so I'm trying to make a script that will list all of the events in my calendar. I basically copied the code from the Developer's Guide ( http://code.google.com/apis/calendar/v3/using.html#calendars). I attached home.php. I'm totally new to Google API programming, and actually pretty new to PHP. I'm sorry for all the questions and confusion! With my current home.php, I get the following error: " Fatal error: Call to a member function listEvents() on a non-object in /home/a2056718/public_html/home.php on line 6" I saw something about using an HTTP GET request to a certain URL, but I wasn't sure exactly what to do. I tried using file_get_contents, but it didn't do anything. I'm sorry! Thanks for all the help.


home.php

Dan Holevoet

unread,
Jan 10, 2012, 2:02:09 PM1/10/12
to google-ca...@googlegroups.com
Hi,

Are you able to share your code here? If you call:

$events = $service->events->listEvents('primary');

directly, without first setting up the service object, you will get an error like that. Make sure the steps to instantiate the client are called from within any page that uses $service.

Thanks,
Dan

On Mon, Jan 9, 2012 at 6:29 PM, JS <js.ste...@gmail.com> wrote:
Okay, so I made my redirect URI bring me to a file ("home.php") in my root directory. I'm trying to just get something to work, so I'm trying to make a script that will list all of the events in my calendar. I basically copied the code from the Developer's Guide ( http://code.google.com/apis/calendar/v3/using.html#calendars). I attached home.php. I'm totally new to Google API programming, and actually pretty new to PHP. I'm sorry for all the questions and confusion! With my current home.php, I get the following error: " Fatal error: Call to a member function listEvents() on a non-object in /home/a2056718/public_html/home.php on line 6" I saw something about using an HTTP GET request to a certain URL, but I wasn't sure exactly what to do. I tried using file_get_contents, but it didn't do anything. I'm sorry! Thanks for all the help.

--
You received this message because you are subscribed to the Google Groups "Google Calendar API" group.

To post to this group, send email to google-ca...@googlegroups.com.
To unsubscribe from this group, send email to google-calendar...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-calendar-api?hl=en.

JS

unread,
Jan 10, 2012, 6:00:54 PM1/10/12
to google-ca...@googlegroups.com
Okay, I'll post my code here. Here is index.php for the root folder:

<?php

session_start();

require_once "google-api-php-client/src/apiClient.php";
require_once "google-api-php-client/src/contrib/apiCalendarService.php";

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

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

?>

Here is the code for index.php in /oauth2callback (it's just a redirect to home.php; is that okay?):

<?php
  //oauth2callback
?>

And here is home.php, back in the root directory, now with the instantiation code in it:

<?php
session_start();

require_once "google-api-php-client/src/apiClient.php";
require_once "google-api-php-client/src/contrib/apiCalendarService.php";

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

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

$events = $service->events->listEvents('primary');

while(true) {
  foreach ($events->getItems() as $event) {
    echo $event->getSummary();
  }
  $pageToken = $events->getNextPageToken();
  if ($pageToken) {
    $optParams = array('pageToken' => $pageToken);
    $events = $service->events->listEvents('primary', $optParams);
  } else {
    break;
  }
}
?>

When I added the code to instantiate the client, I was brought back to the login page. Does this mean that somehow oauth_access_token isn't getting to home.php? How is the token supposed to get to the redirect URI? Is it being passed? I'm sorry for the newbie questions, but, despite all the reading I've done, I still don't quite get it.

Dan Holevoet

unread,
Jan 12, 2012, 12:39:31 PM1/12/12
to google-ca...@googlegroups.com
Hi,

Yes, the token is passed to the callback. By redirecting oauth2callback to home.php you are most likely ignoring the values returned to you by Google. I would ensure that your client object is configured correctly in oauth2callback (making any necessary redirects if it is not), and only then redirect to home.php.

home.php will still need to have the setup code unless you cache the value of $apiClient and reuse it. However, since when you arrive on that page the oauth_access_token should be saved in the current session, your code should successfully make the API call.

Thanks,
Dan

--
You received this message because you are subscribed to the Google Groups "Google Calendar API" group.

To post to this group, send email to google-ca...@googlegroups.com.
To unsubscribe from this group, send email to google-calendar...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-calendar-api?hl=en.

AS

unread,
Dec 7, 2012, 6:55:41 AM12/7/12
to google-ca...@googlegroups.com


I am trying make a client application accessing users calendars using google calendar api v3 but i think my redirect uri is not correct so it's showing
" 404 Not Found

The server can not find the requested page:

perflance.com/demo/googlecalendarapi/google-api-php-client/calendar/calendar1.php?code=4/TLgYBfxDN-DuoG_wGgz-itZhLpTg.8nE4ESxEsd8SXE-sT2ZLcbSbHUzqdgI (port 443)

If you are the account owner, please submit ticket for further information.
Open Ticket "

message after me clicking "allow access" button. Please ask me for details if i am being unclear. All help is appriciated.

aditya sharma

unread,
Dec 7, 2012, 7:44:18 AM12/7/12
to google-ca...@googlegroups.com
<?php
/*
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
session_start();
require_once '../src/Google_Client.php';
require_once '../src/contrib/Google_TasksService.php';

$client = new Google_Client();
// Visit https://code.google.com/apis/console to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
$client->setClientId('197897997960.apps.googleusercontent.com');
$client->setClientSecret('xMx83tLv6a0xgz-_Xy6KZ4d5');
$client->setRedirectUri('https://perflance.com/demo/googlecalendarapi/google-api-php-client/calendar/calendar1.php');
$client->setApplicationName("API Project");
$tasksService = new Google_TasksService($client);

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}

if (isset($_SESSION['access_token'])) {
  $client->setAccessToken($_SESSION['access_token']);
} else {
  $client->setAccessToken($client->authenticate($_GET['code']));
  $_SESSION['access_token'] = $client->getAccessToken();
}

if (isset($_GET['code'])) {
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
?>
<!doctype html>
<html>
<head>
  <title>Tasks API Sample</title>
  <link rel='stylesheet' href='http://fonts.googleapis.com/css?family=Droid+Serif|Droid+Sans:regular,bold' />
  <link rel='stylesheet' href='css/style.css' />
</head>
<body>
<div id='container'>
  <div id='top'><h1>Tasks API Sample</h1></div>
  <div id='main'>
<?php
  $lists = $tasksService->tasklists->listTasklists();
  foreach ($lists['items'] as $list) {
    print "<h3>{$list['title']}</h3>";
    $tasks = $tasksService->tasks->listTasks($list['id']);
  }
?>
  </div>
</div>
</body>
</html>
<?php $_SESSION['access_token'] = $client->getAccessToken(); ?>

This code was avaliable at the post at which I posted my query. Please suggest me the necessary changes that need to be done  in this code to make it run.
Also I would like to know what should i write in "Javascript origins:" in Api Console.


--
You received this message because you are subscribed to the Google Groups "Google Calendar API" group.

yoga aluche

unread,
May 30, 2013, 3:34:38 AM5/30/13
to google-ca...@googlegroups.com
Hi I´ve similar problem´s like you...I can´t resolve this is my code:
What´s is URI???
Help me please¡¡¡¡¡

<?php
require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_CalendarService.php';

session_start();

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

 
$client->setClientSecret('5wg0tyFONd5MEiiP7i8i-Ul8');

$client->setDeveloperKey('AI39si4z7x9seyZlePUH7zk0Il-U71rihOkXyM1to18ea_G2xzmm7SMvtGzol4FeiQuDqDX_7mMIDpm8SHbzvi9cHZCW78wSwA');


 $cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

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

if ($client->getAccessToken()) {
  $calList = $cal->calendarList->listCalendarList();
  print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";

$_SESSION['token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";

SIHAM ESSALEK

unread,
Mar 10, 2014, 7:20:55 AM3/10/14
to google-ca...@googlegroups.com

idd you find the solution cause im having the same probleme here
and it was working well till friday and i relly dnt know what happened when i clic accept the redirection url is right but it adds some aleatory code as
Redirect URIs+?code=4/+aleatory code
and its really frustrating
Reply all
Reply to author
Forward
0 new messages