我正在尝试使用以下代码将文件上传到OneDrive:
$graph_url = 'https://graph.microsoft.com/v1.0/me/drive/root:/filename:/createUploadSession';
$file = file_get_contents('logo.jpeg');
$headers = [
'Content-Type: json',
"Cache-Control: no-cache",
"Pragma: no-cache",
"Authorization: bearer EwAQA61DBAAUcSSzoTJJsy+XrnQXgAKO5cj4yc8AAewWnvfTuLAC2gDmYesGiVawqeIpfS8lJjyCyeXQMrKOu92FA0SK12N/87GBTs4ba2LJ5buqmm9gX18by0jPWzZ5qvpc+BdfMC2wUyiMYxh8QH8BJFBctAVgSaKM1KmhA8mAwb2czyw4OeWdupKH7qSPvjRtwBTTxxX8iIO3IeNbpXDghri9MWRGyVIffGf2Oi+RzQm5jbGuDtXrQa4K80m1o8PHcEkdIEXbaAgIE8oBYCVBQ7fyYxA84b0NjghSRbCZDj5rVbo1MDeBv0J4inmPthEbAMcS3fRSBdFs3+SSz+jZwb3SgSp5Q+VXiRs+keDQHspaoSNt4Ho0+pX94WQDZgAACAk/KNYU0yEY4AHihrs84QKvmWWfIOTYCBudQZD7tKp2ZeOpXEI7q3tOTuwHTv7uUmYxkpGswiNSeW2r9DEBdQ5gBtZ/YCXMPhlBLSxxahrl816zsj/UDi2c6dmB+6J1wk60/hm1Ll6vqn8YbtlyQh9hFLpWejo8+Vofh5/kRUfUSl1v7FIP7YIJLvTyNn1gGy9IMnxYdvba9ACGotVpFTAMNdjz6aQwUk4MJjD7csqYVpyMBh8O7bS40dRMjiUkdhXYvA03L/JV818qgo0W0uwJmoqzPJfe7VB+VekqxPCyVT6A86y/OaD1EsTaLwBILG8eS48Odr/qg7azjUsrdryS+rg7wfQEBEL+kIkS5Ne8eTg1/t2aAXFsUukiIBkTYp+Sold4yhqJU2w/cRDZiz9m3GBltjTS+JiIX2Ll0ABbOwmY9pkc6O8/3T0ZhgVuIigfIILTdMGMu9sNvJtVxYL4gbRlNX+gyMBHZHDo2zktdD5l4vDa+TTgn2Fc3CDUli/qLU1UX2SfJ6+B+epmX/wA32cXVtfDPcm605c18LipDoOVGxzf9RVyNe9e3VCFvR9UBmkobodVfE1VhUfzw/s5CHrjzCZ7ZZDt84Hm6VoWIVZLtYueHCuAaEhLa0gWZTKUSwvu3hCDwZUaAg==",
"Content-Length: ".filesize('logo.jpeg'),
];
$ch = curl_init($graph_url);
curl_setopt($ch, CURLOPT_URL, $graph_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{}');
$server_output = curl_exec($ch);
$info = curl_getinfo($ch);
但结果是总是出现以下错误:
"error": {
"code": "InvalidAuthenticationToken",
"message": "CompactToken parsing failed with error code: 8004920A",
"innerError": {
"request-id": "bab6035c-17c5-45ea-99ac-7c809e446ca2",
"date": "2018-12-22T09:42:03"
}
}
首先,看起来您的令牌不正确。因此,步骤1是验证令牌是否正确。您可以通过复制您的令牌并将其粘贴到Microsoft专门为此作业创建的网站上来实现。 https://jwt.ms
其次,正如微软文档所述,您有2种方式将文件上传到onedrive。
您正尝试直接上传到CreateUploadSession
端点。第一种方法(对于小文件)通过将文件上传到/content
端点,但这只是一个文件上传。所以没有内容标题包含json
。
第二种方法是通过创建上传会话(用一些json响应)然后再做第二次请求将文件上传到你从url获得的端点。
关于这些事情的文档很清楚,我认为你正在使用PHP,因此应该很容易用php模仿提供的curl请求。