gpt4 book ai didi

Azure DevOps REST API SendMail

转载 作者:行者123 更新时间:2023-12-02 23:56:07 27 4
gpt4 key购买 nike

我正在尝试在发布定义的成功阶段后发送邮件。遵循文档在我的阶段中选中了 OAuth 框项目集合服务帐户已添加到构建管理员和发布管理员中。

但是 REST API 的响应是“Azure DevOps 登录页面”这是我的脚本:

$OrganizationName = "myorg"
$ProjectName = "myproj"
$sendmailto = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="791c01181409151c3914181015571a1614" rel="noreferrer noopener nofollow">[email protected]</a>"
$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 token 和对基本返回 400 和不记名返回 401 的授权。

将 $(System.AccessToken) 切换为 $($env:System_AccessToken) 尝试转换为 base64 或不转换。

我缺少什么?

来自的回应 ConsoleLog

最佳答案

这是由$requestBody引起的。请求正文需要由 tfsIds 引用的有效 Azure DevOps 用户。

下面的 PS 脚本适用于我,如果您在管道中运行它,请使用 $(System.AccessToken) 而不是 $PAT

在本地运行并使用 PAT 进行身份验证:

$OrganizationName = "organization"
$ProjectName = "Project"
$sendmailto = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93ebebebd3fefaf0e1fce0fcf5e7bdf0fcfe" rel="noreferrer noopener nofollow">[email protected]</a>"
$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
}

enter image description here

在发布管道中运行并使用 $(System.AccessToken) 进行身份验证:(请注意,由于此脚本在发布期间运行,因此摘要电子邮件将环境显示为 正在进行,即使它是作为发布中的最后一步运行。)

$OrganizationName = "organization"
$ProjectName = "project"
$sendmailto = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84fcfcfcc4e9ede7f6ebf7ebe2f0aae7ebe9" rel="noreferrer noopener nofollow">[email protected]</a>"
$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
}

enter image description here

关于Azure DevOps REST API SendMail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71601908/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com