PHP脚本访问服务器到服务器到谷歌驱动器显示我自己的文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP脚本访问服务器到服务器到谷歌驱动器显示我自己的文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3180字,纯文字阅读大概需要5分钟。
内容图文
![PHP脚本访问服务器到服务器到谷歌驱动器显示我自己的文件](/upload/InfoBanner/zyjiaocheng/819/992d59024f404457b701bb48af8a5fc8.jpg)
我需要帮助才能正确决定如何使用谷歌API.
我想开发一个脚本来连接服务器到服务器(没有任何Oauth2连接)到我的谷歌驱动器文件夹,以便在网页中显示谷歌驱动器文件和文件夹.如果某些文件更新,我还会设置一个cron作业来执行操作.
这可能吗?
在谷歌云平台中,我发现驱动器api具有服务器到服务器的交互.这是正确的解决方案吗?
谢谢!
解决方法:
https://developers.google.com/drive/v3/web/quickstart/php
在这里,您将找到启用API的方法.从那时起,您可以使用该库.
该网站将提供一个样本:
<?php
require __DIR__ . '/vendor/autoload.php';
define('APPLICATION_NAME', 'Drive API PHP Quickstart');
define('CREDENTIALS_PATH', '~/.credentials/drive-php-quickstart.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/drive-php-quickstart.json
define('SCOPES', implode(' ', array(
Google_Service_Drive::DRIVE_METADATA_READONLY)
));
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient() {
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
$accessToken = file_get_contents($credentialsPath);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->authenticate($authCode);
// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, $accessToken);
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, $client->getAccessToken());
}
return $client;
}
/**
* Expands the home directory alias '~' to the full path.
* @param string $path the path to expand.
* @return string the expanded path.
*/
function expandHomeDirectory($path) {
$homeDirectory = getenv('HOME');
if (empty($homeDirectory)) {
$homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH");
}
return str_replace('~', realpath($homeDirectory), $path);
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Drive($client);
// Print the names and IDs for up to 10 files.
$optParams = array(
'pageSize' => 10,
'fields' => "nextPageToken, files(id, name)"
);
$results = $service->files->listFiles($optParams);
if (count($results->getFiles()) == 0) {
print "No files found.\n";
} else {
print "Files:\n";
foreach ($results->getFiles() as $file) {
printf("%s (%s)\n", $file->getName(), $file->getId());
}
}
这将显示10个文件正如他们所说.
这是(在我看来)最好的方式.
内容总结
以上是互联网集市为您收集整理的PHP脚本访问服务器到服务器到谷歌驱动器显示我自己的文件全部内容,希望文章能够帮你解决PHP脚本访问服务器到服务器到谷歌驱动器显示我自己的文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。