0

After folowing the examples from Google PHP quickstart i managed to create a simple page that reads the user list and displays them in a table. Now i am trying to create an ADD form.

All the example are from 2013-2014 and use ID/Secret inside the file.

Is there a way to use the second example with oauth2callback and secrets json files to to add users?

This how outh2callback looks like

<?php
require_once 'google-api-php-client/src/Google/autoload.php';

session_start();

$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
$scopes = array(
    'https://www.googleapis.com/auth/admin.directory.user',
    'https://www.googleapis.com/auth/admin.directory.group'
);
$client->addScope($scopes); 

if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
 $client->authenticate($_GET['code']);
 $_SESSION['access_token'] = $client->getAccessToken();
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}   

And the View page that display the users

<?php
require_once 'google-api-php-client/src/Google/autoload.php';



session_start();

$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$scopes = array(
    'https://www.googleapis.com/auth/admin.directory.user',
    'https://www.googleapis.com/auth/admin.directory.group'
);
$client->addScope($scopes);




if (isset($_SESSION['access_token'])  && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
  $service = new Google_Service_Directory($client);
  $optParams = array(
  'customer' => 'my_customer',
  'orderBy' => 'email',
    );
  $results = $service->users->listUsers($optParams);       }
    else {
        header('Location: oauth2callback.php');
    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
       <title>Google List</title>
       <link href="css/bootstrap.css" rel="stylesheet" media="screen">
        <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="css/bootstrap-theme.min.css" rel="stylesheet"      media="screen">
        <link href="css/style.css" rel="stylesheet" media="screen">
        </head>

<body>



<div class="container-fluid">
<div class="row">
  <div class="col-md-4">&nbsp;</div>
 <div class="col-md-4">

 <form action="index.php" method="POST">
        <pre>
        First Name <input type="text" name='first_name' placeholder='First    Name' required>
        Last Name  <input type="text" name='last_name'  placeholder ='Last Name' required>
        Email      <input type="email" name='email'     placeholder ='Email' required>
                   <button type="submit"  name='save'>Submit</button>
        </pre>


    </form>
     <a href="logout.php?logout"> Logout from Google</a>

  <table class="table table-striped table-bordered">
              <thead>
                <tr>
                  <th><span style="text-align:center;">Nume</span></th>
                  <th><span style="text-align:center;">Mail</span></th
                ></tr>
              </thead>
              <tbody>
                    <?php
                    foreach ($results->getUsers() as $user) {
                        echo '<tr>';
                            echo'<td>'. $user->getName()->getFullName() . '</td>';
                            echo'<td>'. $user->getPrimaryEmail() . '</td>';
                        echo '</tr>';
                                                          }
                     ?>
              </tbody>
            </table></div>
   <div class="col-md-4">&nbsp;</div>
    </div>


    </div>              


    </body>
    </html>
Baron Daniel
  • 43
  • 1
  • 1
  • 10
  • I have found some extra info for using OAuth2.0 for Google Plus login with secrets.json here http://parthjoshi.in/category/programming-languages/php/ – Baron Daniel Feb 19 '16 at 11:17

1 Answers1

0

You're wanting to migrate from OAuth 1.0 to OAuth 2.0.

Here is a protocol level migration guide from Google that's at the protocol level.

Likely to be more helpful though is this OAuth 2.0 guide for the PHP client library. It has the example you're looking for: using the client_secrets.json file.

Peter
  • 5,501
  • 2
  • 26
  • 42