【问题标题】:List and upload to Google Drive API列出并上传到 Google Drive API
【发布时间】:2013-09-10 21:55:26
【问题描述】:

我正在尝试使用 Google drive API 学习一个简单的网络应用程序 (PHP)。

  1. 显示驱动器中的文件列表。
  2. 将新文件添加到驱动器。

代码如下:

<?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-client\src\io\Google_REST 中出现未捕获的异常“Google_ServiceException”和消息“调用 GET https://www.googleapis.com/drive/v2/files: (401) 需要登录”。 php

有人可以帮助纠正我,我也将感谢分享如何插入文档的简单代码。

【问题讨论】:

  • “登录”状态是否需要调用setClientIdsetClientSecret,还是还需要执行authenticate?后者是否被调用(即您的 code 查询字符串是否已填充且正确?

标签: php google-drive-api google-docs-api


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 2018-11-11
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 2020-11-03
    相关资源
    最近更新 更多