[我正在使用此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
中的代码
[SDK似乎不支持客户端凭据OAuth授权。没有那,答案是否定的。您可能需要查看通过Gruzzel HTTP客户端支持此功能的官方Microsoft Graph SDK for PHP:
$guzzle = new \GuzzleHttp\Client();
$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token?api-version=1.0';
$token = json_decode($guzzle->post($url, [
'form_params' => [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'resource' => 'https://graph.microsoft.com/',
'grant_type' => 'client_credentials',
],
])->getBody()->getContents());
$accessToken = $token->access_token;