【问题标题】:Microsoft OneDrive SDK run as cronMicrosoft OneDrive SDK 作为 cron 运行
【发布时间】: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


    【解决方案1】:

    SDK 似乎不支持客户端凭据 OAuth 授权。没有这个,答案是否定的。你可能想看看官方的Microsoft Graph SDK for PHP,它通过Guzzle HTTP 客户端支持这个:

    $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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多