使用 Microsoft Graph 将文件上传到 onedrive api
Uploading file to onedrive using microsoft graph api
我正在尝试使用 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请求数据的编码也如下,我想不出问题...
{"items":
{
"@odata.type":"microsoft.graph.driveItemUploadableProperties",
"@microsoft.graph.conflictBehavior":"rename",
"name":"new_video.mp4"
}
}
根据 msgraph-sdk-php
存储库中给出的 this example,您不需要 json_econde
您的 body 数据。 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);
}
我正在尝试使用 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请求数据的编码也如下,我想不出问题...
{"items":
{
"@odata.type":"microsoft.graph.driveItemUploadableProperties",
"@microsoft.graph.conflictBehavior":"rename",
"name":"new_video.mp4"
}
}
根据 msgraph-sdk-php
存储库中给出的 this example,您不需要 json_econde
您的 body 数据。 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);
}