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

问题描述 投票:0回答:1

尝试使用Powershell更新Freshdesk中的联系人,并且对字段进行简单更新非常有用。现在,我想更新联系人头像/图像,但在multipart / form-data方面苦苦挣扎。

我正在使用Powershell 7 Core。

这是到目前为止的代码

    # Set global variables
$userEmail = "[email protected]"
$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 [email protected]:test -F 'avatar=@/path/to/image.ext' -F 'job_title=Superhero' -X PUT 'https://domain.freshdesk.com/api/v2/contacts/434'

尝试使用Powershell更新Freshdesk中的联系人,并且对字段进行简单更新非常有用。现在,我想更新联系人头像/图像,但在multipart / form-data方面苦苦挣扎。我正在使用...

powershell rest freshdesk
1个回答
0
投票

好了,您现在可以在Powershell核心中使用-form了。Invoke-RestMethod-方法PUT -Uri $ Update_URL -Headers $ HTTPHeaders -Form @ {avatar =(Get-Item $ jpgfile)}

© www.soinside.com 2019 - 2024. All rights reserved.