使用 powershell 更新联系人图像 Freshdesk API
Update contact image Freshdesk API with powershell
尝试使用 Powershell 更新 Freshdesk 中的联系人和简单的字段更新效果很好。
现在我想更新联系人 avatar/image 但在 multipart/form-data.
上苦苦挣扎
我正在使用 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:\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'
好的,看来您现在可以在 powershell 核心中使用 -form 了。
调用-RestMethod -Method PUT -Uri $Update_URL -Headers $HTTPHeaders -Form @{avatar=(Get-Item $jpgfile)}
尝试使用 Powershell 更新 Freshdesk 中的联系人和简单的字段更新效果很好。 现在我想更新联系人 avatar/image 但在 multipart/form-data.
上苦苦挣扎我正在使用 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:\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'
好的,看来您现在可以在 powershell 核心中使用 -form 了。 调用-RestMethod -Method PUT -Uri $Update_URL -Headers $HTTPHeaders -Form @{avatar=(Get-Item $jpgfile)}