使用Powershell更新联系人图像Freshdesk API



尝试使用Powershell更新Freshdesk中的联系人并简单地更新字段效果很好。 现在我想更新 conatact 头像/图像,但正在努力处理多部分/表单数据。

我使用的是Powershell 7 Core。

这是到目前为止的代码

# 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:tempscriptsFreshdeskUserName.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 请求必须将其内容类型设置为多部分/表单数据。

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'

好的,你现在可以在Powershell核心中使用-form了。 Invoke-RestMethod -Method PUT -uri $Update_URL -Headers $HTTPHeaders -form @{avatar=(get-Item $jpgfile(}

最新更新