尝试使用 Powershell 更新 Freshdesk 中的联系人和简单的字段更新效果很好。现在我想更新联系人头像/图像,但在处理多部分/表单数据时遇到了困难。
我正在使用 Powershell 7 核心。
这是到目前为止的代码
# Set global variables
$userEmail = "user.name@domain.se"
$myDomain = "domain"
$APIKey = '1234678910111213'
$EncodedCredentials = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $APIKey,$null)))
$HTTPHeaders = @{}
$HTTPHeaders.Add('Authorization', ("Basic {0}" -f $EncodedCredentials))
$jpgfile= 'C:\temp\scripts\Freshdesk\UserName.jpg'
#Get ID for User
$GET_URL = "https://$myDomain.freshdesk.com/api/v2/contacts?email=$userEmail"
$user=Invoke-RestMethod -Method GET -Uri $GET_URL -Headers $HTTPHeaders
$fd_id = $user.id
#Update User
$HTTPHeaders.Add('Content-Type', 'multipart/form-data')
$Image = [convert]::ToBase64String((get-content $jpgfile -AsByteStream))
$Update_URL="https://$myDomain.freshdesk.com/api/v2/contacts/$fd_id"
$UserAttributes = @{}
$customAttributes = @{}
$avatarAttributes = @{}
$UserAttributes.Add('name', 'User Name')
$UserAttributes.Add('job_title' , 'Boss')
$UserAttributes.Add('email' , $userEmail)
$customAttributes.Add('department', 'IT')
$customAttributes.Add('Subdepartment', 'Development')
$UserAttributes += @{'custom_fields' = $customAttributes}
$UserAttributes += @{'avatar' = $image}
$JSON = $UserAttributes | ConvertTo-Json
Invoke-RestMethod -Method PUT -Uri $Update_URL -Headers $HTTPHeaders -Body $JSON
来自https://developers.freshdesk.com/api/#contacts的 API 文档
此 API 请求的 Content-Type 必须设置为 multipart/form-data。
Sample code | Curl
curl -v -u user@yourcompany.com:test -F 'avatar=@/path/to/image.ext' -F 'job_title=Superhero' -X PUT 'https://domain.freshdesk.com/api/v2/contacts/434'