通过 Web 请求回复 Azure Bot - 此请求的授权已被拒绝
Reply to an Azure Bot via Web Request - Authorization has been denied for this request
我正在尝试通过 Web 请求回复 Azure 机器人,但我尝试的一切,甚至发起对话,都会导致 Authorization has been denied for this request.
使用 PowerShell,我 运行 以下内容:
# Variables
$botServiceUrl = "https://smba.trafficmanager.net/au/v3/conversations"
$botBody = '{}'
$clientID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$clientSecret = "This_is-a.Secret"
$headers = @{}
$scope = "https://api.botframework.com/.default";
$tenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$tokenBody = @{
'grant_type' = 'client_credentials'
'client_id' = $clientID
'client_secret' = $clientSecret
'scope' = $scope
}
$tokenEndpoint = {https://login.microsoftonline.com/{0}/oauth2/v2.0/token} -f $tenantID
# Get Token
$params = @{
ContentType = 'application/x-www-form-urlencoded'
Headers = @{'accept'='application/json'}
Body = $tokenBody
Method = 'POST'
URI = $tokenEndpoint
}
$token = Invoke-RestMethod @params
# Send message to Azure Bot
$headers.Add("Authorization","$($token.token_type) "+ " " + "$($token.access_token)")
$params = @{
ContentType = 'application/json'
Headers = $headers
Body = $botBody
Method = 'POST'
URI = $botServiceUrl
}
$result = Invoke-WebRequest @params
任何帮助/建议,非常感谢。
不确定其根本原因,但如果您只想使用 PowerShell 脚本与您的机器人进行通信,您可以使用 direct line 3.0 API 作为解决方法。
尝试以下命令:
$clientID = ""
$clientSecret = ""
$directLinekey = ""
$StartConversationUrl = "https://webchat.botframework.com/v3/directline/conversations"
# Get Token
$params = @{
ContentType = 'application/x-www-form-urlencoded'
Headers = @{"Authorization"="Bearer $directLinekey" }
Method = 'POST'
URI = $StartConversationUrl
}
$result = Invoke-RestMethod @params
$conversationID = $result.conversationId
$token = $result.token
# Send message to Azure Bot
$postMessageURL = "https://webchat.botframework.com/v3/directline/conversations/$conversationID/activities"
$botBody = @'
{
"locale": "en-EN",
"type":"message",
"from": {
"id": "user1"
},
"text":"hello"
}
'@
$sendMessageParams = @{
ContentType = 'application/json'
Headers = @{"Authorization"="Bearer $token"}
Method = 'POST'
Body= $botBody
URI = $postMessageURL
}
$messageResult = Invoke-RestMethod @sendMessageParams
$id= $messageResult.id
$getMessageURL = "https://webchat.botframework.com/v3/directline/conversations/$conversationID/activities"
$getMessageParams = @{
ContentType = 'application/json'
Headers = @{"Authorization"="Bearer $token"}
Method = 'get'
URI = $getMessageURL
}
$allActs = Invoke-RestMethod @getMessageParams
foreach($act in $allActs.activities){
$act.from
$act.text
echo('-------')
}
我这边的结果:
好的 - 找到问题了。
我需要使用 Uri https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token
来检索专门用于机器人的令牌。
例如:
$clientID = ""
$clientSecret = ""
$recipientId = ""
# Get Token
$tokenEndpoint = "https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token"
$tokenBody = @{
'grant_type' = 'client_credentials'
'client_id' = $clientID
'client_secret' = $clientSecret
'scope' = "https://api.botframework.com/.default"
}
$params = @{
ContentType = 'application/x-www-form-urlencoded'
Headers = @{"Host" = "login.microsoftonline.com"}
Body = $tokenBody
Method = 'POST'
URI = $tokenEndpoint
}
$result = Invoke-RestMethod @params
开始对话:
$conversationUrl = "https://smba.trafficmanager.net/apis/v3/conversations"
$conversationBody = '{
"bot": {
"id": "28:'+$clientID+'",
},
"isGroup": false,
"members": [
{
"id": "29:'+$recipientId+'"
}
],
"topicName": "News Alert"
}'
$params = @{
ContentType = 'application/json'
Headers = @{"Authorization" = "$($result.token_type) $($result.access_token)" }
Body = $conversationBody
Method = 'POST'
URI = $conversationUrl
}
$result = Invoke-RestMethod @params
$conversationID = $result.id
我还没有在 PowerShell 中解决如何发送消息的问题,但我在逻辑应用程序中的工作流程可以通过 HTTP Web 请求向 Teams 中的机器人发送消息。
谢谢!
我正在尝试通过 Web 请求回复 Azure 机器人,但我尝试的一切,甚至发起对话,都会导致 Authorization has been denied for this request.
使用 PowerShell,我 运行 以下内容:
# Variables
$botServiceUrl = "https://smba.trafficmanager.net/au/v3/conversations"
$botBody = '{}'
$clientID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$clientSecret = "This_is-a.Secret"
$headers = @{}
$scope = "https://api.botframework.com/.default";
$tenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$tokenBody = @{
'grant_type' = 'client_credentials'
'client_id' = $clientID
'client_secret' = $clientSecret
'scope' = $scope
}
$tokenEndpoint = {https://login.microsoftonline.com/{0}/oauth2/v2.0/token} -f $tenantID
# Get Token
$params = @{
ContentType = 'application/x-www-form-urlencoded'
Headers = @{'accept'='application/json'}
Body = $tokenBody
Method = 'POST'
URI = $tokenEndpoint
}
$token = Invoke-RestMethod @params
# Send message to Azure Bot
$headers.Add("Authorization","$($token.token_type) "+ " " + "$($token.access_token)")
$params = @{
ContentType = 'application/json'
Headers = $headers
Body = $botBody
Method = 'POST'
URI = $botServiceUrl
}
$result = Invoke-WebRequest @params
任何帮助/建议,非常感谢。
不确定其根本原因,但如果您只想使用 PowerShell 脚本与您的机器人进行通信,您可以使用 direct line 3.0 API 作为解决方法。
尝试以下命令:
$clientID = ""
$clientSecret = ""
$directLinekey = ""
$StartConversationUrl = "https://webchat.botframework.com/v3/directline/conversations"
# Get Token
$params = @{
ContentType = 'application/x-www-form-urlencoded'
Headers = @{"Authorization"="Bearer $directLinekey" }
Method = 'POST'
URI = $StartConversationUrl
}
$result = Invoke-RestMethod @params
$conversationID = $result.conversationId
$token = $result.token
# Send message to Azure Bot
$postMessageURL = "https://webchat.botframework.com/v3/directline/conversations/$conversationID/activities"
$botBody = @'
{
"locale": "en-EN",
"type":"message",
"from": {
"id": "user1"
},
"text":"hello"
}
'@
$sendMessageParams = @{
ContentType = 'application/json'
Headers = @{"Authorization"="Bearer $token"}
Method = 'POST'
Body= $botBody
URI = $postMessageURL
}
$messageResult = Invoke-RestMethod @sendMessageParams
$id= $messageResult.id
$getMessageURL = "https://webchat.botframework.com/v3/directline/conversations/$conversationID/activities"
$getMessageParams = @{
ContentType = 'application/json'
Headers = @{"Authorization"="Bearer $token"}
Method = 'get'
URI = $getMessageURL
}
$allActs = Invoke-RestMethod @getMessageParams
foreach($act in $allActs.activities){
$act.from
$act.text
echo('-------')
}
我这边的结果:
好的 - 找到问题了。
我需要使用 Uri https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token
来检索专门用于机器人的令牌。
例如:
$clientID = ""
$clientSecret = ""
$recipientId = ""
# Get Token
$tokenEndpoint = "https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token"
$tokenBody = @{
'grant_type' = 'client_credentials'
'client_id' = $clientID
'client_secret' = $clientSecret
'scope' = "https://api.botframework.com/.default"
}
$params = @{
ContentType = 'application/x-www-form-urlencoded'
Headers = @{"Host" = "login.microsoftonline.com"}
Body = $tokenBody
Method = 'POST'
URI = $tokenEndpoint
}
$result = Invoke-RestMethod @params
开始对话:
$conversationUrl = "https://smba.trafficmanager.net/apis/v3/conversations"
$conversationBody = '{
"bot": {
"id": "28:'+$clientID+'",
},
"isGroup": false,
"members": [
{
"id": "29:'+$recipientId+'"
}
],
"topicName": "News Alert"
}'
$params = @{
ContentType = 'application/json'
Headers = @{"Authorization" = "$($result.token_type) $($result.access_token)" }
Body = $conversationBody
Method = 'POST'
URI = $conversationUrl
}
$result = Invoke-RestMethod @params
$conversationID = $result.id
我还没有在 PowerShell 中解决如何发送消息的问题,但我在逻辑应用程序中的工作流程可以通过 HTTP Web 请求向 Teams 中的机器人发送消息。
谢谢!