You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MS Graph API PHP SDK创建SharePoint文件夹失败求助

问题:使用MS Graph API PHP SDK创建SharePoint文件夹时返回400错误

我参考MS Learn的示例,尝试使用MS Graph API PHP SDK在SharePoint站点中创建文件夹,代码如下:

public function createFolder($folderName)
{
    try {
        $requestBody = new DriveItem();
        $requestBody->setName($folderName);
        $folder = new Folder();
        $requestBody->setFolder($folder);
        $additionalData = [
            '@microsoft.graph.conflictBehavior' => 'rename',
        ];      
        $requestBody->setAdditionalData($additionalData);

        $response = $this->graphServiceClient->drives()->byDriveId($this->driveId)->items()->byDriveItemId("root")->children()->post($requestBody)->wait();

    } catch (Exception $e) {
        throw new Exception("Error: " . $e->getMessage());
    }
}

调用post()方法时抛出异常,返回400响应码,错误信息为:

[item] Read called with an open stream or textreader. Please close any open streams or text readers before calling Read.

堆栈跟踪信息:

#0 \vendor\microsoft\kiota-serialization-json\src\JsonParseNode.php(108): Microsoft\Graph\Generated\Models\ODataErrors\ODataError::createFromDiscriminatorValue(Object(Microsoft\Kiota\Serialization\Json\JsonParseNode))
#1 \vendor\microsoft\kiota-http-guzzle\src\GuzzleRequestAdapter.php(637): Microsoft\Kiota\Serialization\Json\JsonParseNode->getObjectValue(Array)
#2 \vendor\microsoft\kiota-http-guzzle\src\GuzzleRequestAdapter.php(156): Microsoft\Kiota\Http\GuzzleRequestAdapter->throwFailedResponse(Object(GuzzleHttp\Psr7\Response), Array, Object(OpenTelemetry\API\Trace\NonRecordingSpan))
#3 \vendor\php-http\promise\src\FulfilledPromise.php(39): Microsoft\Kiota\Http\GuzzleRequestAdapter->Microsoft\Kiota\Http\{closure}(Object(GuzzleHttp\Psr7\Response))
#4 \vendor\microsoft\kiota-http-guzzle\src\GuzzleRequestAdapter.php(164): Http\Promise\FulfilledPromise->then(Object(Closure))
#5 \vendor\microsoft\microsoft-graph\src\Generated\Drives\Item\Items\Item\Children\ChildrenRequestBuilder.php(84): Microsoft\Kiota\Http\GuzzleRequestAdapter->sendAsync(Object(Microsoft\Kiota\Abstractions\RequestInformation), Array, Array)
#6 \common\class\GraphAssihub.php(172): Microsoft\Graph\Generated\Drives\Item\Items\Item\Children\ChildrenRequestBuilder->post(Object(Microsoft\Graph\Generated\Models\DriveItem))
#7 \cp\ajax\pagetest.php(169): GraphAssihub->createFolder('new_folder')
#8 {main}

已做排查:

  • 通过Graph Explorer调用POST /drives/{drive-id}/items/root/children接口可正常执行,权限无问题
  • PHP调用$this->graphServiceClient->drives()->byDriveId($this->driveId)->items()->byDriveItemId("root")->children()->get()->wait()能正常返回根目录下的所有项目
  • 尝试在现有文件夹内创建新文件夹,仍出现相同异常

更新于2023年12月6日09:23:00 UTC
追踪ChildrenRequestBuilder内部调用流程:

ChildrenRequestBuilder.post()
-> GuzzleRequestAdapter.sendAsync()
-> GuzzleRequestAdapter.getHttpResponseMessage()
  $request = $this->authenticationProvider->authenticateRequest($requestInfo, $additionalAuthContext);
  $request->then(
    ...
    $psrRequest = $this->getPsrRequestFromRequestInformation($requestInfo, $httpResponseSpan);
    ...
  )->then(function (ResponseInterface $response)
    ...
  )

$psrRequest信息:

GuzzleHttp\Psr7\Request {#212 
  -method: "POST"
  -requestTarget: null
  -uri: GuzzleHttp\Psr7\Uri {#224 
    -scheme: "https"
    -userInfo: ""
    -host: "graph.microsoft.com"
    -port: null
    -path: "/v1.0/drives/xxxxxxxxxxxxx/items/root/children"
    -query: ""
    -fragment: ""
    -composedComponents: null
  }
  -headers: array:4 [
    "Host" => array:1 [
      0 => "graph.microsoft.com"
    ]
    "accept" => array:1 [
      0 => "application/json;q=1"
    ]
    "content-type" => array:1 [
      0 => "application/json"
    ]
    "authorization" => array:1 [
      0 => "Bearer xxxxxxxx"
    ]
  ]
  -headerNames: array:4 [
    "accept" => "accept"
    "content-type" => "content-type"
    "authorization" => "authorization"
    "host" => "Host"
  ]
  -protocol: "1.1"
  -stream: GuzzleHttp\Psr7\Stream {#73 
    -stream: stream resource @643 
      wrapper_type: "PHP"
      stream_type: "TEMP"
      mode: "w+b"
      unread_bytes: 0
      seekable: true
      uri: "php://temp"
      options: []
    }
    -size: 135
    -seekable: true
    -readable: true
    -writable: true
    -uri: "php://temp"
    -customMetadata: []
  ]
}

$response信息:

GuzzleHttp\Psr7\Response {#262 
  -reasonPhrase: "Bad Request"
  -statusCode: 400
  -headers: array:8 [
    "Cache-Control" => array:1 [
      0 => "no-store, no-cache"
    ]
    "Transfer-Encoding" => array:1 [
      0 => "chunked"
    ]
    "Content-Type" => array:1 [
      0 => "application/json"
    ]
    "Strict-Transport-Security" => array:1 [
      0 => "max-age=31536000"
    ]
    "request-id" => array:1 [
      0 => "xxxxxxx"
    ]
    "client-request-id" => array:1 [
      0 => "xxxxxxx"
    ]
    "x-ms-ags-diagnostic" => array:1 [
      0 => "{"ServerInfo":{"DataCenter":"Switzerland North","Slice":"E","Ring":"5","ScaleUnit":"002","RoleInstance":"xxxxx"}}"
    ]
    "Date" => array:1 [
      0 => "Wed, 06 Dec 2023 08:43:38 GMT"
    ]
  ]
  -headerNames: array:8 [
    "cache-control" => "Cache-Control"
    "transfer-encoding" => "Transfer-Encoding"
    "content-type" => "Content-Type"
    "strict-transport-security" => "Strict-Transport-Security"
    "request-id" => "request-id"
    "client-request-id" => "client-request-id"
    "x-ms-ags-diagnostic" => "x-ms-ags-diagnostic"
    "date" => "Date"
  ]
  -protocol: "1.1"
  -stream: GuzzleHttp\Psr7\Stream {#207 
    -stream: stream resource @646 
      wrapper_type: "PHP"
      stream_type: "TEMP"
      mode: "w+b"
      unread_bytes: 0
      seekable: true
      uri: "php://temp"
      options: []
    }
    -size: null
    -seekable: true
    -readable: true
    -writable: true
    -uri: "php://temp"
    -customMetadata: []
  ]
}

请问我哪里操作出错了?


内容的提问来源于stack exchange,提问作者Michele Bonacina

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 22:34:53