如何通過powershell獲取請求的authtoken (How to obtain authtoken for request via powershell)


問題描述

如何通過powershell獲取請求的authtoken (How to obtain authtoken for request via powershell)

我需要向通過 powershell 發出的請求添加授權令牌。在 c# 中,我可以像這樣獲取令牌:

private static string GetAccessToken()
    {
        AuthenticationContext authContext = new AuthenticationContext(((MyApp)Application.Current).AuthorityAddress);
        Task<AuthenticationResult> resultTask = authContext.AcquireTokenAsync(
            (myAuthServerAddress,
            clientId,
            redirectUri,
            new Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters(PromptBehavior.Auto, false));

        resultTask.Wait();
        return resultTask.Result.AccessToken;
    }

我將如何在 powershell 中執行此操作?我需要將該令牌添加為標頭:

Authorization: Bearer blahblahblahtokenblahblah

參考解法

方法 1:

Try run the following code in PowerShell:

function Get‑AzureRmCachedAccessToken()
{
  $ErrorActionPreference = 'Stop'

  if(‑not (Get‑Module AzureRm.Profile)) {
    Import‑Module AzureRm.Profile
  }
  $azureRmProfileModuleVersion = (Get‑Module AzureRm.Profile).Version
  # refactoring performed in AzureRm.Profile v3.0 or later
  if($azureRmProfileModuleVersion.Major ‑ge 3) {
    $azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
    if(‑not $azureRmProfile.Accounts.Count) {
      Write‑Error "Ensure you have logged in before calling this function."    
    }
  } else {
    # AzureRm.Profile < v3.0
    $azureRmProfile = [Microsoft.WindowsAzure.Commands.Common.AzureRmProfileProvider]::Instance.Profile
    if(‑not $azureRmProfile.Context.Account.Count) {
      Write‑Error "Ensure you have logged in before calling this function."    
    }
  }

  $currentAzureContext = Get‑AzureRmContext
  $profileClient = New‑Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azureRmProfile)
  Write‑Debug ("Getting access token for tenant" + $currentAzureContext.Subscription.TenantId)
  $token = $profileClient.AcquireAccessToken($currentAzureContext.Subscription.TenantId)
  $token.AccessToken
}

Get‑AzureRmCachedAccessToken

From: https://gallery.technet.microsoft.com/scriptcenter/Easily‑obtain‑AccessToken‑3ba6e593

方法 2:

You can do like this.

$bearerAuthValue = "Bearer {token}"

$headers = @{ Authorization = $bearerAuthValue }

Invoke Web Request like this

Invoke‑WebRequest ‑uri "https://api.google.com/user" ‑Headers $headers

You can find further info here https://foxdeploy.com/2015/11/02/using‑powershell‑and‑oauth/

(by SKLAKstack247Venky)

參考文件

  1. How to obtain authtoken for request via powershell (CC BY‑SA 2.5/3.0/4.0)

#powershell #httpwebrequest #HTTP #C#






相關問題

玩弄 C# 加密 (Playing around with C# encryption)

查找具有特定 startname 的特定服務 (Finding specific services with specific startname)

試圖從事件日誌中獲取數據到電子郵件中的 html (Trying to get data from event logs into html in email)

如何通過powershell獲取請求的authtoken (How to obtain authtoken for request via powershell)

如何在錯誤時繼續使用 &$var 調用可執行文件 (How to continue on error a call to an executable using &$var)

在同一範圍內從 c# 調用多個 powershell 命令 (Invoke multiple powershell commands from c# on the same scope)

如何在 Powershell 二進制模塊(.Net Standard 和 Nuget)中處理公共和私有依賴項和打包 (How to handle public and private dependencies and packaging in Powershell binary module (.Net Standard & Nuget))

如何使用參數(描述空白)獲取廣告中的所有 OU - 沒有描述? (How to get all OU in Ad with parameter (description blank ) - without description?)

遠程服務器返回錯誤:(400) 錯誤請求。在 C:\Program Files\WindowsPowerShell\Modules\CosmosDB\3.1.0.293\CosmosDB.psm1 (The remote server returned an error: (400) Bad Request. At C:\Program Files\WindowsPowerShell\Modules\CosmosDB\3.1.0.293\CosmosDB.psm1)

Powershell 檢查文件類型 (Powershell check file type)

服務器待重啟 (Server Pending Reboot)

PowerShell 的 Invoke-WebRequest 在 Docker 容器中不起作用 (PowerShell's Invoke-WebRequest not working within a Docker Container)







留言討論