Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

powershell - Reply to an Azure Bot via Web Request - Authorization has been denied for this request

I'm trying to reply to an Azure Bot via a Web Request, but everthing I try, even initiate a converstation, results in a Authorization has been denied for this request.

Using PowerShell, I run the following:

# 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

Any help / suggestions, much appreciated.

question from:https://stackoverflow.com/questions/65557783/reply-to-an-azure-bot-via-web-request-authorization-has-been-denied-for-this-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Not sure the root reason for it, but if you just want to have a communication with your bot using PowerShell Script, you can use direct line 3.0 API to do so as a workaround.

Try command below:

$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('-------')
}

Result on my side:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...