我正在尝试使用powershell和API来更新单元格,但是我收到错误:1004您无权执行此操作。我已经删除了ID。 。 。
我有30天的试用期。在我可以使用API进行更新或什么之前,我是否需要购买某种级别的支持?工作表在我的帐户上,我是所有者。 Write Sheets角色是否包含在Owner?
中我正在使用powershell 5.1来发出http请求 - 这是我的脚本。它首先获取正确的工作表ID,然后将我感兴趣的行和列放入哈希,然后尝试更新工作表,但是虽然我是所有者,但我未经授权。
# 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 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;
....
答案 0 :(得分:1)
您应该能够使用由试用Smartsheet帐户拥有的API访问令牌进行API调用,并且您当然可以使用您拥有的API访问令牌更新您拥有的工作表。
我怀疑1004
错误响应是由$put_headers
的内容格式不正确引起的。即,Smartsheet无法准确解析您尝试在$put_headers
中指定的标头,以便识别授权标头并读取其值。当Smartsheet在入站API请求中没有看到授权标头时,它会回复1004 You are not authorized to perform this action.
错误响应。
要解决此问题,我建议您使用像Fiddler这样的工具来检查出站“更新行”请求,特别注意请求中存在哪些标头。然后,如果您发现请求中没有授权标头,则需要弄清楚如何在PowerShell中指定多个请求标头,并相应地更新您的代码以及如何设置值$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")
答案 1 :(得分:0)
谢谢@ kim-brandl
或者如果要在单个命令中构建标头,则需要使用如下语法:
$put_headers = @{"Authorization" = "Bearer " + $apiKey; "Content-Type" = "application/json" }
另请注意,您的json有效负载存在一些问题,包括:
我建议你先用postman这样的工具测试有效载荷。