本帖最后由 小白龙 于 2024-12-16 14:42 编辑
回复 8# went
大佬, 我加了一个函数, 用来创建文件的分享链接, 但是总是报下面的错, 能指点一下吗? 多谢
创建分享链接文档: https://learn.microsoft.com/zh-c ... w=odsp-graph-online
正在处理文件: /n8n/Img/eb020b6f-8201-46d9-90b0-ada9c6f59b7c_0.png
响应内容: {"error":{"code":"invalidRequest","message":"API not found","innerError":{"date":"2024-12-16T06:37:05","request
-id":"808f12f5-3fad-48ec-8b3c-3720bd278845","client-request-id":"808f12f5-3fad-48ec-8b3c-3720bd278845"}}}
API 请求失败,状态码:
错误信息:{"error":{"code":"invalidRequest","message":"API not found","innerError":{"date":"2024-12-16T06:37:05","request-
id":"808f12f5-3fad-48ec-8b3c-3720bd278845","client-request-id":"808f12f5-3fad-48ec-8b3c-3720bd278845"}}}
文件: /n8n/Img/eb020b6f-8201-46d9-90b0-ada9c6f59b7c_0.png 的分享链接是:
正在处理文件: /n8n/Img/小花猫.png
响应内容: {"error":{"code":"invalidRequest","message":"API not found","innerError":{"date":"2024-12-16T06:37:05","request
-id":"5a94b51e-b01b-466d-8829-7dddc6fb5c44","client-request-id":"5a94b51e-b01b-466d-8829-7dddc6fb5c44"}}}
API 请求失败,状态码:
错误信息:{"error":{"code":"invalidRequest","message":"API not found","innerError":{"date":"2024-12-16T06:37:05","request-
id":"5a94b51e-b01b-466d-8829-7dddc6fb5c44","client-request-id":"5a94b51e-b01b-466d-8829-7dddc6fb5c44"}}}
文件: /n8n/Img/小花猫.png 的分享链接是:
- # 创建分享链接
- function OneDrive-CreateLink($item_id){
- $uri = "https://graph.microsoft.com/v1.0/drive/items/$item_id/createLink"
- $body = @{
- type = "view" # 可选值: 'view', 'edit'
- scope = "anonymous" # 可选值: 'anonymous', 'organization'
- } | ConvertTo-Json
-
- $whr.Open('POST', $uri, $false)
- $whr.SetRequestHeader('Authorization', $Script:authorization)
- $whr.SetRequestHeader('Content-Type', 'application/json')
- $whr.Send($body)
-
- # 输出响应内容,帮助调试
- $responseText = $whr.ResponseText
- Write-Host "响应内容: $responseText"
-
- if ($whr.StatusCode -eq 200) {
- $response = $responseText | ConvertFrom-Json
- if ($response.link -ne $null) {
- return $response.link.webUrl
- } else {
- Write-Host "创建分享链接失败,返回没有链接信息。"
- return ""
- }
- } else {
- Write-Host "API 请求失败,状态码:$($whr.StatusCode)"
- Write-Host "错误信息:$responseText"
- return ""
- }
- }
-
- #---------------------------------------------------------------------------------------------初始化token
- if([System.IO.File]::Exists('refresh_token.txt')){
- Write-Host '使用refresh_token更新access_token' -ForegroundColor Yellow
- $Script:token = OneDrive-RefreshToken
- } else {
- Write-Host '获取token' -ForegroundColor Yellow
- $Script:token = OneDrive-GetToken
- }
- if($Script:token -eq $null){
- Write-Host 'token获取失败' -ForegroundColor Red
- exit
- } else {
- Write-Host 'token获取成功' -ForegroundColor Green
- }
- $Script:authorization = 'bearer {0}' -f $Script:token.access_token
- $Script:root = OneDrive-GetItemInfo -item_path '/'
- #--------------------------------------------------------------------------------------------
- # 枚举文件并为每个文件生成分享链接
- function list($p){
- OneDrive-GetList -item_path $p | foreach {
- $s = '{0}/{1}' -f $p.TrimEnd('/'),$_.name
- Write-Host "正在处理文件: $s"
-
- # 获取文件信息
- $fileInfo = OneDrive-GetItemInfo -item_path $s
- $itemId = $fileInfo.id
-
- # 创建共享链接
- $link = OneDrive-CreateLink -item_id $itemId
- Write-Host "文件: $s 的分享链接是: $link"
-
- # 递归列出子文件夹中的文件
- list -p $s
- }
- }
-
- list -p '/'
复制代码
|