smartsheet api 更新行错误 1004。您未获得授权

smartsheet api update rows error 1004. you are not authorized

我正在尝试使用 powershell 和 API 更新单元格,但出现错误:1004 您无权执行此操作。我已经删除了 ID。 . .

我有 30 天的试用期。在使用 API 进行更新之前,我是否需要购买某种级别的支持? sheet 在我的帐户上,我是所有者。 Write Sheets 角色是否不包含在 Owner 中?

我正在使用 powershell 5.1 发出 http 请求 – 这是我的脚本。它首先获取正确的sheet ID,然后将我感兴趣的行和列放入散列中,然后尝试更新sheet,但是虽然我是所有者,但我是未授权的。

# access account of jjj.mmm@ddd.com
$apiKey = "3ccfgk..."
$url = "https://api.smartsheet.com/2.0/sheets/"
$get_headers = @{"Authorization" = "Bearer " + $apiKey}
$put_headers = @{"Authorization" = "Bearer " + $apiKey, "Content-Type: application/json"  }

# get all the sheets
$response = Invoke-RestMethod -Uri $url -Headers $get_headers
$rd = $response.data
$json = ConvertTo-Json $rd

# find the Forecast sheet
$sheet_id = $rd | Where {$_.name -eq "Forecast Intermediary Sheet"} | Select -ExpandProperty id
"Sheet ID: $sheet_id"

#get the Forecast sheet
$surl = "$url$sheet_id"
$surl
$response = Invoke-RestMethod -Uri $surl -Headers $get_headers
$response | Format-List

# iterate over the rows and get the active ones
$active_rows = @{}
foreach ($row in $response.rows) {
    if ($row.rowNumber -ge 14) {
        if ($row.cells[2].value -Match "Active") {
            $active_rows.Add($row.cells[2].value, $row.id)
        }
    }
}

# get the col_ids by date
$date_cols = @{}
foreach ($c in $response.columns) {
    if ($c.title -as [datetime]) {
        $d = $c.title -replace "/20", "/"
        $ds = [datetime]::parseexact($d, 'mm/dd/yy', $null)
        $date_cols.Add($ds.Tostring("yyyy-mm-dd"), $c.id)
    }

}


"UPDATE:"
$rowid = $active_rows["SSL PS Active"]
$colid = $date_cols["2017-11-01"]
"row: $rowid, col: $colid"

$json = '[{ "id": "'+$rowid+'", "cells": [{"columnId": "'+$colid+'","value": "23"} }]'
"JSON: $json"
$purl = "$surl/rows"
"PUT URL: $purl"
$r = Invoke-RestMethod -Method "PUT" -uri $purl -Headers $put_headers -Body $json
$r

下面是脚本的输出。该错误似乎表明它在语法上是正确的,但 OWNER 权限不足。

Sheet ID: 5724...
https://api.smartsheet.com/2.0/sheets/5724....
UPDATE:
row: 5668..., col: 5118...
JSON: [{ "id": "5668...", "cells": [{"columnId": "5118...","value": "23"} }]
PUT URL: https://api.smartsheet.com/2.0/sheets/5724.../rows


Invoke-RestMethod : {
  "errorCode" : 1004,
  "message" : "You are not authorized to perform this action.",
  "refId" : "14a7o8lu9sfyj"
}
At \winfiles\jmoore\powershell\ss_api.ps1:58 char:6
+ $r = Invoke-RestMethod -Method "PUT" -uri $purl -Headers $put_headers ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], 
    WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodComand

这是我的 sheet

的元数据
Sheet ID: 5724...
https://api.smartsheet.com/2.0/sheets/5724....


id                         : 5724...
name                       : Forecast Intermediary Sheet
version                    : 2
totalRowCount              : 37
accessLevel                : OWNER
effectiveAttachmentOptions : {GOOGLE_DRIVE, DROPBOX, ONEDRIVE, EGNYTE...}
ganttEnabled               : False
dependenciesEnabled        : False
resourceManagementEnabled  : False
cellImageUploadEnabled     : True
userSettings               : @{criticalPathEnabled=False; displaySummaryTasks=True}
permalink                  : https://app.smartsheet.com/b/home?lx=LCTEj6F0xWNKWWxFUuLH0w
createdAt                  : 2017-11-22T22:34:51Z
modifiedAt                 : 2017-11-22T22:34:51Z
columns                    : {@{id=6985...; index=0; title=Column1; type=TEXT_NUMBER; primary=True; 
                             validation=False; width=64}, @{id=1356...; index=1; title=Column2; 
                             type=PICKLIST; options=System.Object[]; validation=False; width=64}, 
                             @{id=5859...; index=2; title=Column3; type=TEXT_NUMBER; validation=False; 



....

您应该能够使用试用 Smartsheet 帐户拥有的 API 访问令牌进行 API 调用,并且您肯定可以更新 sheet 使用您拥有的 API 访问令牌。

我怀疑 1004 错误响应是由 $put_headers 的内容格式不正确引起的。即,Smartsheet 无法准确解析您试图在 $put_headers 中指定的 header 以识别 授权 header 并读取它的值。当 Smartsheet 在入站 API 请求中没有看到 Authorization header 时,它会返回 1004 You are not authorized to perform this action. 错误响应。

要解决此问题,建议您使用 Fiddler 等工具检查出站 "Update Rows" 请求,特别注意请求中存在哪些 header。然后,如果您发现请求中缺少 Authorization header,您将需要弄清楚如何在 PowerShell 中指定多个请求 header,并相应地更新您的代码,重新设置 $put_headers 的值。

更新(添加 PowerShell 代码):

我不是 PowerShell 专家,但您可以尝试替换此行:

$put_headers = @{"Authorization" = "Bearer " + $apiKey, "Content-Type: application/json"}

改用这些行:

$put_headers = @{}
$put_headers.Add("Authorization", "Bearer " + $apiKey)
$put_headers.Add("Content-Type", "application/json")

感谢@kim-brandl

或者,如果您想在单个命令中构建 headers,则需要使用如下语法: $put_headers = @{"Authorization" = "Bearer " + $apiKey; "Content-Type" = "application/json" }

另请注意,您的 json 负载存在一些问题,包括:

  • 括号不匹配
  • 外面有额外的方括号(body是一个object,不是数组)
  • 列和行 ID 必须是数字,而不是字符串

我建议你先用postman之类的工具测试payload。