使用oauth2callback.php将用户添加到Directory API,而不是将客户端/机密添加到文件?

时间:2016-02-18 13:54:53

标签: php google-admin-sdk

在从PHP PHP快速入门开始的示例之后,我设法创建了一个简单的页面,用于读取用户列表并将其显示在表格中。 现在我正在尝试创建一个ADD表单。

所有示例均来自2013-2014,并在文件中使用ID / Secret。

有没有办法使用oauth2callback的第二个例子并秘密json文件来添加用户?

这个outh2callback如何看起来像

<?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));
}   

显示用户的“查看”页面

<?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>

1 个答案:

答案 0 :(得分:0)

您希望从OAuth 1.0迁移到OAuth 2.0。

以下是来自Google的protocol level migration guide,它位于协议级别。

对于PHP客户端库,这个OAuth 2.0 guide可能更有帮助。它有你正在寻找的例子:使用client_secrets.json文件。