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

Google API OAuth2:删除共享驱动器文件遇404错误求助

问题描述

使用PowerShell脚本将CSV文件更新到Google共享驱动器,上传功能正常,但添加删除模块时,执行HTTP DELETE方法出现404错误:

删除文件: file.csv,ID: 1xxxxxxxxxxxxxxxxxxxxx-4
删除文件ID 1xxxxxxxxxxxxxxxxxxxx-4失败。错误: 远程服务器返回错误: (404) Not Found。

已知条件:

  • 文件确实存在
  • 账号拥有完全权限
  • 使用的权限范围为https://www.googleapis.com/auth/drive
  • 文件通过PowerShell和Google OAuth2上传,删除同一文件时触发错误

附原PowerShell代码:

# Set the Google Auth parameters
$params = @{
    Uri = 'https://accounts.google.com/o/oauth2/token'
    Body = @{
        refresh_token = $RefreshToken
        client_id = $ClientID
        client_secret = $ClientSecret
        grant_type = 'refresh_token'
    }
    Method = 'Post'
    ContentType = 'application/x-www-form-urlencoded'
}

# Get the access token
$accessToken = (Invoke-RestMethod @params).access_token

# Specify the shared drive ID and file name
$teamDriveId = "xxxxxxxxxxxxxxxPVA"
$fileName = "file.csv"


# Step 1: Search for specific file name within the shared drive without specifying parents
$searchUri = "https://www.googleapis.com/drive/v3/files?q=name='$fileName' and trashed=false&corpora=drive&driveId=$teamDriveId&supportsAllDrives=true&includeItemsFromAllDrives=true"
$searchHeaders = @{
    "Authorization" = "Bearer $accessToken"
}
$searchResult = Invoke-RestMethod -Uri $searchUri -Method Get -Headers $searchHeaders

Write-Host "Search response: $($searchResult | ConvertTo-Json)"

# Step 2: Delete files if found
if ($searchResult.files) {
    foreach ($file in $searchResult.files) {
        $fileId = $file.id
        Write-Host "Deleting file: $fileName with ID: $fileId"

        $deleteUri = "https://www.googleapis.com/drive/v3/files/$fileId?supportsAllDrives=true"
        try {
            Invoke-RestMethod -Uri $deleteUri -Method Delete -Headers $searchHeaders
            Write-Host "Deleted file ID $fileId successfully."
        }
        catch {
            Write-Host "Failed to delete file ID $fileId. Error: $_"
        }
    }
} else {
    Write-Host "No instances of $fileName found in the shared drive."
}
解决建议
  • 补充删除请求的必填参数:针对共享驱动器文件,删除请求除supportsAllDrives=true外,需额外添加driveId参数,明确指定文件所在的共享驱动器ID,避免API无法定位文件。
  • 验证搜索结果的准确性:检查搜索响应输出,确认返回的file.id属于目标共享驱动器内的文件,排除个人驱动器中同名文件被误匹配的情况。
  • 确认权限令牌的有效范围:调用https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=$accessToken验证当前令牌的权限列表,确保包含https://www.googleapis.com/auth/drive或https://www.googleapis.com/auth/drive.file权限。
  • 尝试移至回收站替代直接删除:若直接删除仍报错,可先将文件标记为已删除(移入回收站),示例请求:
    $updateUri = "https://www.googleapis.com/drive/v3/files/$fileId?supportsAllDrives=true&driveId=$teamDriveId"
    $body = @{ trashed = $true } | ConvertTo-Json
    Invoke-RestMethod -Uri $updateUri -Method Patch -Headers $searchHeaders -Body $body -ContentType "application/json"
    
  • 开启调试查看详细错误:在Invoke-RestMethod中添加-Verbose参数,查看完整请求/响应内容,定位隐藏的API错误提示。
修正后的示例代码
# Set the Google Auth parameters
$params = @{
    Uri = 'https://accounts.google.com/o/oauth2/token'
    Body = @{
        refresh_token = $RefreshToken
        client_id = $ClientID
        client_secret = $ClientSecret
        grant_type = 'refresh_token'
    }
    Method = 'Post'
    ContentType = 'application/x-www-form-urlencoded'
}

# Get the access token
$accessToken = (Invoke-RestMethod @params).access_token

# Specify the shared drive ID and file name
$teamDriveId = "xxxxxxxxxxxxxxxPVA"
$fileName = "file.csv"

# Step 1: Search for specific file name within the shared drive
# 优化查询,明确限定文件所在共享驱动器
$searchUri = "https://www.googleapis.com/drive/v3/files?q=name='$fileName' and trashed=false and driveId='$teamDriveId'&corpora=drive&driveId=$teamDriveId&supportsAllDrives=true&includeItemsFromAllDrives=true"
$searchHeaders = @{
    "Authorization" = "Bearer $accessToken"
}
$searchResult = Invoke-RestMethod -Uri $searchUri -Method Get -Headers $searchHeaders

Write-Host "Search response: $($searchResult | ConvertTo-Json)"

# Step 2: Delete files if found
if ($searchResult.files) {
    foreach ($file in $searchResult.files) {
        $fileId = $file.id
        Write-Host "Deleting file: $fileName with ID: $fileId"

        # 添加driveId参数到删除请求
        $deleteUri = "https://www.googleapis.com/drive/v3/files/$fileId?supportsAllDrives=true&driveId=$teamDriveId"
        try {
            Invoke-RestMethod -Uri $deleteUri -Method Delete -Headers $searchHeaders -Verbose
            Write-Host "Deleted file ID $fileId successfully."
        }
        catch {
            Write-Host "Failed to delete file ID $fileId."
            Write-Host "Error message: $($_.Exception.Message)"
            # 输出API返回的详细错误内容
            if ($_.Exception.Response) {
                $responseStream = $_.Exception.Response.GetResponseStream()
                $reader = New-Object System.IO.StreamReader($responseStream)
                $responseContent = $reader.ReadToEnd()
                Write-Host "API error response: $responseContent"
            }
        }
    }
} else {
    Write-Host "No instances of $fileName found in the shared drive."
}

内容的提问来源于stack exchange,提问作者Chris Lynch

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 10:53:10