繁体   English   中英

Google云端硬盘API-获取共享文件下载链接

[英]Google Drive API - Fetch Shared Files Download Links

我是Google云端硬盘的新手,并上传了许多文件。 如果知道下载链接,我已更改了要共享的所有文件的状态。

我想使用PHP生成文件的所有直接下载链接的列表。 PHP脚本在我的本地主机上运行,​​我只需要将数组保存到文本文件中以备后用。

我很难使Oauth正常工作,但是遇到了看起来像我需要的脚本。 我设置了我的服务帐户,并有我的服务帐户电子邮件和.p12文件。

我下载了Google PHP客户端代码库,并在本地主机上设置了测试脚本。 这就是我所拥有的

require_once "Google/Client.php";
require_once "Google/Service/Drive.php";
require_once "Google/Service/Oauth2.php";
require_once "Google/Auth/AssertionCredentials.php";

session_start();


function buildService($userEmail) {

$SERVICE_ACCOUNT_EMAIL = '12345@developer.gserviceaccount.com';
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = '12345-privatekey.p12';

$key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
$auth = new Google_AssertionCredentials(
$SERVICE_ACCOUNT_EMAIL,
array('https://www.googleapis.com/auth/drive'),
$key);
$auth->sub = $userEmail;
$client = new Google_Client();
$client->setUseObjects(true);
$client->setAssertionCredentials($auth);

return new Google_DriveService($client);
}

//... function retrieveAllFiles($service)

$service = buildService("myemail@gmail.com");
$allFiles = retrieveAllFiles($service);
print_r($allFiles);

我正在处理这个例子

http://stackoverflow.com/questions/21046631/google-drive-api-php-cant-list-files

和这个

https://developers.google.com/drive/web/delegation#instantiate_a_drive_service_object

我不确定该怎么做,我已经添加了必需的库,但是由于PHP脚本中不存在以下功能,因此即将出现

Google_AssertionCredentials

setUseObjects

Google_DriveService

retrieveAllFiles

我是否缺少明显的图书馆? 在示例中,它们的名称不同,但是我猜自从有了更新以来,基本名称就发生了变化……我花了很多时间在Google云端硬盘和Oauth上进行阅读,但运气不佳。 我对该脚本的唯一目标是获取直接下载链接的列表。 我可以手动完成,但是文件太多。

任何帮助都会很棒。

谢谢。


*编辑:*


所以这就是我试图获取令牌的方法:

我正在遵循此快速入门指南:

https://developers.google.com/drive/web/quickstart/quickstart-php

这是我的代码

<?php

require_once 'Google/client.php';
require_once 'Google/Service/drive.php';

defined('STDIN') or define('STDIN', fopen('php://stdin', 'r')); //I had to add this part as I was getting an undefined error for STDIN 

$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('xxx.apps.googleusercontent.com');
$client->setClientSecret('xxx');
$client->setRedirectUri('http://localhost/test/fetch.php'); //same as registered one
$client->setScopes(array('https://www.googleapis.com/auth/drive'));

$service = new Google_Service_Drive($client);

$authUrl = $client->createAuthUrl();

//Request authorization
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));

// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);

这导致错误

Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Invalid code' in C:\Apache24\htdocs\test\Google\Auth\OAuth2.php:95 Stack trace: #0 C:\Apache24\htdocs\test\Google\Client.php(135): Google_Auth_OAuth2->authenticate('') #1 C:\Apache24\htdocs\test\new.php(26): Google_Client->authenticate('') #2 {main} thrown in C:\Apache24\htdocs\test\Google\Auth\OAuth2.php on line 95

有什么想法吗 ?

谢谢。


另一个编辑


因此,由于新的Drive PHP库没有任何文档或示例,因此我已恢复到旧的Drive PHP库。

这是我尝试获取令牌并获取直接下载文件链接的尝试

require_once 'google-old/google-api-php-client/src/Google_Client.php';
require_once 'google-old/google-api-php-client/src/contrib/Google_DriveService.php';

$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('xx.apps.googleusercontent.com');
$client->setClientSecret('xx');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');

$client->setScopes(array('https://www.googleapis.com/auth/drive'));

$service = new Google_DriveService($client);

$authUrl = $client->createAuthUrl();

//Request authorization
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));

// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);

$getAll = retrieveAllFiles($service);

print "<pre>";
print_r($getAll);
print "</pre>";

/**
 * Retrieve a list of File resources.
 *
 * @param Google_DriveService $service Drive API service instance.
 * @return Array List of Google_DriveFile resources.
 */
function retrieveAllFiles($service) {
$result = array();
$pageToken = NULL;

do {
    try {
        $parameters = array();
        if ($pageToken) {
            $parameters['pageToken'] = $pageToken;
        }
        $files = $service->files->listFiles($parameters);

        $result = array_merge($result, $files->getItems());
        $pageToken = $files->getNextPageToken();
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
        $pageToken = NULL;
    }
} while ($pageToken);
return $result;
}

我现在面临的问题是我被困在“将代码粘贴到您的应用程序”部分中。

$authCode = trim(fgets(STDIN));

似乎不想执行。

我也相信文件获取代码只会获取文件名,而不是直接下载链接。 我找不到这个例子。

任何建议将不胜感激。

谢谢。

我正在我的本地主机上对此进行测试。

首先,将您对OAuth的了解与对云端硬盘的了解分开。 作为一个综合的问题,这很困难,但是作为两个独立的问题,这很简单。

如果是OAuth,你需要知道的一切是这样的一个网页上https://developers.google.com/accounts/docs/OAuth2WebServer

阅读并理解该页面后,如果要使用库进行调用,请继续。 恕我直言,OAuth库的功能并不出色,但ymmv。 无论有没有图书馆,了解您所发生的事情都是至关重要的。

破解OAuth之后,您将得到一个闪亮的access_token,您可以将其放入Drive API,如果调用rar API,则作为http标头;如果使用的是其中一个库,则包装在凭证对象中。

替换STDIN的东西。

放进去:

if (!isset($_GET['code'])) {
    print "Please visit:\n$authUrl\n\n";
    print "Please enter the auth code:\n";
} else { $authCode = ''; }
//$authCode = trim(fgets(STDIN));

$authCode = $_GET['code'];

Google的代码就其本身而言是错误的。 完成此操作后,我开始创建文件(它仍在弹出错误,但正在生成文件)。

将其file_get_contents行替换为:

$data = 'blahblah blah';//file_get_contents('document.txt');

然后你去。

暂无
暂无

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

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