0

我正在尝试使用 microsoft graph api 和 laravel 中的 php sdk 实现将视频文件上传到 onedrive。

代码详情如下

// Get the access token from the cache
    $tokenCache = new TokenCache();
    $accessToken = $tokenCache->getAccessToken();

    $file_name = 'new_video.mp4';
    $media_path = storage_path('video.mp4');

    // Create a Graph client
    $graph = new Graph();
    $graph->setAccessToken($accessToken);

          //create post request
    $postData = json_encode([ 'items' =>
                    [
                        "@odata.type" => "microsoft.graph.driveItemUploadableProperties",
                        "@microsoft.graph.conflictBehavior" => "rename",
                        "name" => $file_name
                    ]
                ]);
    //dd(json_encode($postData));

    try {
        $post = $graph->createRequest('POST', '/drive/root/:{item-path}:/createUploadSession')->attachBody($postData)->upload($media_path);

    } catch (\Illuminate\Http\Client\RequestException $e) {
        dd($e);
    }

不知道什么也应该进入项目路径,我尝试使用文件夹名称但出现错误,我也尝试排除项目路径但仍然出现错误。

这是我得到的错误

Client error: `POST https://graph.microsoft.com/v1.0/drive/root/createUploadSession` resulted in a `400 Bad Request` response: {
 "error": {
 "code": "BadRequest",
 "message": "Unable to read JSON request payload. Please ensure Content-T (truncated...)

请求数据的 Json Encode 也如下,我无法弄清楚问题...

{"items":
  {
    "@odata.type":"microsoft.graph.driveItemUploadableProperties",
    "@microsoft.graph.conflictBehavior":"rename",
    "name":"new_video.mp4"
 }
}
4

1 回答 1

0

根据存储库上给出的这个例子msgraph-sdk-php,你不需要json_econde你的身体数据。sdk 会为你做的。

所以你的代码应该是这样的:

    // Get the access token from the cache
    $tokenCache = new TokenCache();
    $accessToken = $tokenCache->getAccessToken();

    $file_name = 'new_video.mp4';
    $media_path = storage_path('video.mp4');

    // Create a Graph client
    $graph = new Graph();
    $graph->setAccessToken($accessToken);

          //create post request
    $postData = [ 'items' =>
                    [
                        "@odata.type" => "microsoft.graph.driveItemUploadableProperties",
                        "@microsoft.graph.conflictBehavior" => "rename",
                        "name" => $file_name
                    ]
                ];

    try {
        $post = $graph->createRequest('POST', '/drive/root/:{item-path}:/createUploadSession')->attachBody($postData)->upload($media_path);

    } catch (\Illuminate\Http\Client\RequestException $e) {
        dd($e);
    }
于 2020-05-28T17:18:50.680 回答