[英]List and upload to Google Drive API
我正在尝试使用Google Drive API学习一个简单的Web应用程序(PHP)。
代码如下:
<?php
session_start();
$url_array = explode('?', 'http://'.$_SERVER ['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$url = $url_array[0];
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();
$client->setClientId(''); // Kept my id
$client->setClientSecret(''); //kept my key
$client->setRedirectUri($url);
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
if (isset($_GET['code'])) {
$_SESSION['accessToken'] = $client->authenticate($_GET['code']);
header('location:'.$url);exit;
} elseif (!isset($_SESSION['accessToken'])) {
$client->authenticate();
}
到这里为止一切正常。 该应用程序通过身份验证,然后重定向回我的应用程序。 现在,我尝试显示文件列表。
问题代码:
$service = new Google_DriveService($client);
$result = array();
$files = $service->files->listFiles();
$result = array_merge($result, $files->getItems());
print_r($result);
我收到此错误:
错误:C:\\ wamp \\ www \\ Sample \\ google-api-php中出现未捕获的异常“ Google_ServiceException”,消息为“调用GET https://www.googleapis.com/drive/v2/files时出错:(401)需要登录” -client \\ src \\ io \\ Google_REST.php
有人可以帮忙纠正我吗,我也很乐意分享简单的代码如何插入文档。
在$ client-> authenticate();之后添加此行:$ client-> setAccessToken($ accessToken);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.