简体   繁体   English

列出并上传到Google Drive API

[英]List and upload to Google Drive API

I am trying to learn a simple web app (PHP) using Google drive API. 我正在尝试使用Google Drive API学习一个简单的Web应用程序(PHP)。

  1. Display list of files in the drive. 显示驱动器中的文件列表。
  2. Add a new file to the drive. 将新文件添加到驱动器。

Code as follows: 代码如下:

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

It works fine till here. 到这里为止一切正常。 The app gets authenticated and redirects back to my app. 该应用程序通过身份验证,然后重定向回我的应用程序。 Now, I try displaying list of files. 现在,我尝试显示文件列表。

Problem code: 问题代码:

 $service = new Google_DriveService($client);
 $result = array();
  $files = $service->files->listFiles();
  $result = array_merge($result, $files->getItems());
 print_r($result);

I get this error: 我收到此错误:

Error: Uncaught exception 'Google_ServiceException' with message 'Error calling GET https://www.googleapis.com/drive/v2/files : (401) Login Required' in C:\\wamp\\www\\Sample\\google-api-php-client\\src\\io\\Google_REST.php 错误:C:\\ wamp \\ www \\ Sample \\ google-api-php中出现未捕获的异常“ Google_ServiceException”,消息为“调用GET https://www.googleapis.com/drive/v2/files时出错:(401)需要登录” -client \\ src \\ io \\ Google_REST.php

Can someone please help in correcting me and also I would be thankful in sharing simple code how to insert a doc. 有人可以帮忙纠正我吗,我也很乐意分享简单的代码如何插入文档。

在$ client-> authenticate();之后添加此行:$ client-> setAccessToken($ accessToken);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM