【发布时间】:2016-05-15 16:40:26
【问题描述】:
我正在尝试创建一个脚本来将我的文件上传到 Google 云端硬盘。当我运行它时,它运行良好。这是工作代码:
public function uploadToDrive()
{
$client = new Google_Client();
$client->setClientId(Mage::getStoreConfig('mtm_google/drive/client_id'));
$client->setClientSecret(Mage::getStoreConfig('mtm_google/drive/client_secret'));
$client->setRedirectUri(Mage::getStoreConfig('mtm_google/drive/redirect_uri'));
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
session_start();
if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
} else {
$client->setAccessToken($_SESSION['access_token']);
}
$service = new Google_Service_Drive($client);
$file = new Google_Service_Drive_DriveFile();
$file->setName('analysis.csv');
$file->setDescription('A test document');
$data = file_get_contents('temp.csv');
$service->files->create($file, array(
'data' => $data,
'mimeType' => 'text/csv',
'uploadType' => 'multipart'
));
echo 'SUCCESS!';
unlink('temp.csv');
} else {
$authUrl = $client->createAuthUrl();
header('Location: ' . $authUrl);
exit();
}
}
这个脚本应该可以从控制台执行,而不是从某个 URL 执行。当我尝试从控制台执行此操作时,没有任何反应,因为会话变量和重定向是它无法处理的。如何修改此脚本以便从控制台执行?
【问题讨论】:
-
消除对仅在 Web 环境中可用的事物的任何依赖?杀死$_SESSION,杀死
header(),等等等等。 -
给我完整的代码,我会在我的本地主机上试试
-
@Monty 我已经解决了它(使用不同的脚本),但真的很感谢你的努力。谢谢!
标签: php shell oauth-2.0 google-drive-api executable