One thing I would check here is the asynchronous nature of the Graph Copy API.
The driveItem/copy endpoint doesn't perform the replacement synchronously. Microsoft documents it as returning 202 Accepted with a Location header containing a monitor URL. The actual copy/replacement is then processed in the background.
So if you're seeing a 401 Unauthenticated while the SharePoint file is sometimes actually replaced, I would capture the complete HTTP response, including:
- HTTP status code
- Response body
Location header
- Correlation/request IDs
- Whether the replacement completed afterward
Also verify that you're using the documented syntax:
POST /sites/{site-id}/drive/items/{item-id}/copy?@microsoft.graph.conflictBehavior=replace
For SharePoint/OneDrive for Business, replace is supported for file conflicts.
If you receive a 202, follow the monitor URL and check its final status rather than treating the initial response as the final result.
For reliability, I'd also test an alternative approach: if your real requirement is simply replace the contents of an existing SharePoint file, consider updating the existing DriveItem's content rather than using copy. The Graph upload/content APIs support replacing file content and can use upload sessions for larger files.
Since Graph Explorer is also returning 401, I'd additionally test the exact request against a known-good file and verify the token's permissions and the target item's access. That helps distinguish an authentication/authorization problem from an issue specific to the Copy operation.
The fact that the file is actually replaced is an important clue, so I wouldn't assume the 401 alone means the entire operation failed. The actual Graph response + copy monitor status should tell you where the failure is occurring.