我正在尝试使用谷歌登录。我有以下两个文件:
的login.php:
public Form1()
{
InitializeComponent();
ToolStripItem activeToolStripItem = null;
menuStrip1.Paint += (o, e) => activeToolStripItem?.Select();
menuStrip1.ItemClicked += (o, e) => activeToolStripItem = e.ClickedItem;
}
而index.php是:
<?php
require_once __DIR__.'/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('client_secret.json');
$client->setRedirectUri('http://localhost/search/index.php/');
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
$client->setAccessType("offline");
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));
}
我正在使用此处的确切代码: https://developers.google.com/api-client-library/php/auth/web-app
但是,我被重定向到相同的Google身份验证页面。看来<?php
require_once __DIR__.'/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfig('client_secret.json');
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
print_r($_SESSION);
$client->setAccessToken($_SESSION['access_token']);
$drive = new Google_Service_Drive($client);
$files = $drive->files->listFiles(array())->getItems();
echo json_encode($files);
} else {
$redirect_uri = 'http://localhost/search/login.php/';
// echo "I am in else.";
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
不起作用。
请帮帮我。