【发布时间】:2019-12-14 18:26:24
【问题描述】:
您好,我正在使用此 OneDrive SDK。 https://github.com/krizalys/onedrive-php-sdk 。我正在使用 PHP。我需要的是创建一个 cronjob,它将在 OneDrive 中获取我的文件并将其保存到我的本地目录。按照 SDK 中的教程,它可以正常工作。但是,SDK 的工作方式是需要将您重定向到 Microsoft 帐户登录页面才能进行身份验证。
这需要浏览器。我的问题,是否可以通过像 cron 一样在后端运行它来做到这一点?我似乎找不到使用 SDK 的方法。在 Google 中,他们会为您提供访问服务的密钥,而无需每次都登录。我不确定 OneDrive。
$localPath = __DIR__.'/uploads/';
$today = date('Y-m-d');
$folder = $client->getMyDrive()->getDriveItemByPath('/'.$today);
echo "<pre>";
try {
$files = $folder->getChildren();
$createdDirectory = $localPath.$today;
// Check if directory exist
if(is_dir($createdDirectory)){
echo "\n"." Directory ".$createdDirectory." already exists, creating a new one..";
// Create new directory
$uuid1 = Uuid::uuid1();
$createdDirectory = $createdDirectory.$uuid1->toString();
echo "\n".$createdDirectory." created..";
}
// Create directory
mkdir($createdDirectory);
echo "\n".count($files)." found for ".$today;
// Loop thru files inside the folder
foreach ($files as $file){
$save = $file->download();
// Write file to directory
$fp = fopen($createdDirectory.'/'.$file->name, 'w');
fwrite($fp, $save);
echo("\n File ".$file->name." saved!");
}
} catch (Exception $e){
die("\n".$e->getMessage());
}
die("\n Process Complete!");
我的代码类似于redirect.php中的代码
【问题讨论】:
标签: php microsoft-graph-api onedrive