Azure Devops REST API SendMail
Azure Devops REST API SendMail
我正在尝试在发布定义的成功阶段后发送邮件。
按照文档
OAuth 框在我的阶段被选中
项目集合服务帐户已添加到构建管理员和发布管理员。
但是 REST API 的响应是“Azure DevOps 登录页面”
这是我的脚本:
$OrganizationName = "myorg"
$ProjectName = "myproj"
$sendmailto = "example@mail.com"
$mysubject = "Test Mail Subjcet"
$mailbody = "Mail body to test it works with azure rest api"
$PAT="MYPAT"
$Token = "$(System.AccessToken)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$Token"))
$HeaderMail = @{
Authorization = "Bearer $encodedCreds"
}
##send mail
$urimail = "https://${OrganizationName}.vsrm.visualstudio.com/${ProjectName}/_apis/Release/sendmail/$($env:RELEASE_RELEASEID)?api-version=3.2-preview.1"
$requestBody =
@"
{
"senderType":1,
"to":{"tfsIds":[$sendmailto]},
"body":"${mailbody}",
"subject":"${mysubject}"
}
"@
Try {
Invoke-RestMethod -Uri $urimail -Body $requestBody -Method POST -ContentType "application/json" -Headers $HeaderMail
}
Catch {
$_.Exception
}
测试:
尝试使用 3.2 版本和 7.1
PAT 令牌和对 Basic return 400 的授权以及 Bearer return 401.
将 $(System.AccessToken) 切换为 $($env:System_AccessToken) trygin 以转换为 base64 和不转换。
我错过了什么?
回复来自
ConsoleLog
这是$requestBody引起的。请求正文需要 tfsIds
.
引用的有效 Azure DevOps 用户
下面的 PS 脚本适合我,如果您正在 运行 管道中使用它,那么请使用 $(System.AccessToken)
而不是 $PAT
。
运行 在本地并使用 PAT
:
进行身份验证
$OrganizationName = "organization"
$ProjectName = "Project"
$sendmailto = "xxx@microsoft.com"
$mysubject = "Test Mail Subjcet"
$PAT="xxx"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$PAT"))
$HeaderMail = @{
Authorization = "Basic $encodedCreds"
}
#Get the tfsid
$userentitlementurl = "https://vsaex.dev.azure.com/${OrganizationName}/_apis/userentitlements?api-version=7.1-preview.1"
$response = Invoke-RestMethod -Uri $userentitlementurl -Method Get -Headers $HeaderMail
#Filter by sendmailto
$tfsid = ($response.value| where {$_.user.mailAddress -eq $sendmailto}).id
Write-Host $tfsid
##send mail
$urimail = "https://vsrm.dev.azure.com/${OrganizationName}/${ProjectName}/_apis/Release/sendmail/168?api-version=7.1-preview.1"
$requestBody =
@"
{
"senderType": 1,
"to": {
"tfsIds": [
"$tfsid"
],
"emailAddresses": []
},
"subject": "$mysubject",
"sections": [
5,
0,
1,
2,
4
]
}
"@
Try {
Invoke-RestMethod -Uri $urimail -Body $requestBody -Method POST -ContentType "application/json" -Headers $HeaderMail
}
Catch {
$_.Exception
}
运行 在发布管道中并使用 $(System.AccessToken)
进行身份验证:(请注意,因为此脚本在发布期间正在 运行,因此摘要电子邮件将环境显示为 IN PROGRESS
即使它是 运行 作为发布的最后一步。)
$OrganizationName = "organization"
$ProjectName = "project"
$sendmailto = "xxx@microsoft.com"
$mysubject = "Test Mail Subjcet"
$HeaderMail = @{
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
#Get the tfsid
$userentitlementurl = "https://vsaex.dev.azure.com/${OrganizationName}/_apis/userentitlements?api-version=7.1-preview.1"
$response = Invoke-RestMethod -Uri $userentitlementurl -Method Get -Headers $HeaderMail
#Filter by sendmailto
$tfsid = ($response.value| where {$_.user.mailAddress -eq $sendmailto}).id
Write-Host $tfsid
##send mail
$urimail = "$env:SYSTEM_TEAMFOUNDATIONSERVERURI$env:SYSTEM_TEAMPROJECT/_apis/Release/sendmail/$($env:RELEASE_RELEASEID)?api-version=7.1-preview.1"
$requestBody =
@"
{
"senderType": 1,
"to": {
"tfsIds": [
"$tfsid"
],
"emailAddresses": []
},
"subject": "$mysubject",
"sections": [
5,
0,
1,
2,
4
]
}
"@
Try {
Invoke-RestMethod -Uri $urimail -Body $requestBody -Method POST -ContentType "application/json" -Headers $HeaderMail
}
Catch {
$_.Exception
}
我正在尝试在发布定义的成功阶段后发送邮件。 按照文档 OAuth 框在我的阶段被选中 项目集合服务帐户已添加到构建管理员和发布管理员。
但是 REST API 的响应是“Azure DevOps 登录页面” 这是我的脚本:
$OrganizationName = "myorg"
$ProjectName = "myproj"
$sendmailto = "example@mail.com"
$mysubject = "Test Mail Subjcet"
$mailbody = "Mail body to test it works with azure rest api"
$PAT="MYPAT"
$Token = "$(System.AccessToken)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$Token"))
$HeaderMail = @{
Authorization = "Bearer $encodedCreds"
}
##send mail
$urimail = "https://${OrganizationName}.vsrm.visualstudio.com/${ProjectName}/_apis/Release/sendmail/$($env:RELEASE_RELEASEID)?api-version=3.2-preview.1"
$requestBody =
@"
{
"senderType":1,
"to":{"tfsIds":[$sendmailto]},
"body":"${mailbody}",
"subject":"${mysubject}"
}
"@
Try {
Invoke-RestMethod -Uri $urimail -Body $requestBody -Method POST -ContentType "application/json" -Headers $HeaderMail
}
Catch {
$_.Exception
}
测试: 尝试使用 3.2 版本和 7.1
PAT 令牌和对 Basic return 400 的授权以及 Bearer return 401.
将 $(System.AccessToken) 切换为 $($env:System_AccessToken) trygin 以转换为 base64 和不转换。
我错过了什么?
回复来自 ConsoleLog
这是$requestBody引起的。请求正文需要 tfsIds
.
下面的 PS 脚本适合我,如果您正在 运行 管道中使用它,那么请使用 $(System.AccessToken)
而不是 $PAT
。
运行 在本地并使用 PAT
:
$OrganizationName = "organization"
$ProjectName = "Project"
$sendmailto = "xxx@microsoft.com"
$mysubject = "Test Mail Subjcet"
$PAT="xxx"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$PAT"))
$HeaderMail = @{
Authorization = "Basic $encodedCreds"
}
#Get the tfsid
$userentitlementurl = "https://vsaex.dev.azure.com/${OrganizationName}/_apis/userentitlements?api-version=7.1-preview.1"
$response = Invoke-RestMethod -Uri $userentitlementurl -Method Get -Headers $HeaderMail
#Filter by sendmailto
$tfsid = ($response.value| where {$_.user.mailAddress -eq $sendmailto}).id
Write-Host $tfsid
##send mail
$urimail = "https://vsrm.dev.azure.com/${OrganizationName}/${ProjectName}/_apis/Release/sendmail/168?api-version=7.1-preview.1"
$requestBody =
@"
{
"senderType": 1,
"to": {
"tfsIds": [
"$tfsid"
],
"emailAddresses": []
},
"subject": "$mysubject",
"sections": [
5,
0,
1,
2,
4
]
}
"@
Try {
Invoke-RestMethod -Uri $urimail -Body $requestBody -Method POST -ContentType "application/json" -Headers $HeaderMail
}
Catch {
$_.Exception
}
运行 在发布管道中并使用 $(System.AccessToken)
进行身份验证:(请注意,因为此脚本在发布期间正在 运行,因此摘要电子邮件将环境显示为 IN PROGRESS
即使它是 运行 作为发布的最后一步。)
$OrganizationName = "organization"
$ProjectName = "project"
$sendmailto = "xxx@microsoft.com"
$mysubject = "Test Mail Subjcet"
$HeaderMail = @{
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
#Get the tfsid
$userentitlementurl = "https://vsaex.dev.azure.com/${OrganizationName}/_apis/userentitlements?api-version=7.1-preview.1"
$response = Invoke-RestMethod -Uri $userentitlementurl -Method Get -Headers $HeaderMail
#Filter by sendmailto
$tfsid = ($response.value| where {$_.user.mailAddress -eq $sendmailto}).id
Write-Host $tfsid
##send mail
$urimail = "$env:SYSTEM_TEAMFOUNDATIONSERVERURI$env:SYSTEM_TEAMPROJECT/_apis/Release/sendmail/$($env:RELEASE_RELEASEID)?api-version=7.1-preview.1"
$requestBody =
@"
{
"senderType": 1,
"to": {
"tfsIds": [
"$tfsid"
],
"emailAddresses": []
},
"subject": "$mysubject",
"sections": [
5,
0,
1,
2,
4
]
}
"@
Try {
Invoke-RestMethod -Uri $urimail -Body $requestBody -Method POST -ContentType "application/json" -Headers $HeaderMail
}
Catch {
$_.Exception
}