[英]Add users to Directory API with oauth2callback.php instead of adding client/secret to file?
按照Google PHP快速入门中的示例进行操作后,我设法创建了一个简单的页面,该页面读取用户列表并将其显示在表格中。 现在,我正在尝试创建一个添加表单。
所有示例均来自2013-2014,并在文件内使用ID /秘密。
有没有办法将第二个示例与oauth2callback和secrets 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"> </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"> </div>
</div>
</div>
</body>
</html>
您想从OAuth 1.0迁移到OAuth 2.0。
这是Google在协议级别的协议级别迁移指南 。
不过,可能更有用的是此针对PHP客户端库的OAuth 2.0指南 。 它具有您要查找的示例:使用client_secrets.json文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.