2016-11-22 14 views
0

我需要添加一個授權令牌給我通過powershell進行的請求。在C#中,我可以得到這樣的令牌:如何通過powershell獲取authtoken的請求

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 

回答

0

您可以這樣做。

$bearerAuthValue = "Bearer {token}" 

$headers = @{ Authorization = $bearerAuthValue } 

調用Web請求這樣

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

您可以在這裏找到進一步的信息https://foxdeploy.com/2015/11/02/using-powershell-and-oauth/

+0

如何獲得令牌關係嗎? – SKLAK

+0

我認爲你必須使用基本令牌調用身份驗證API,並從該調用的響應中獲取訪問令牌並使用它來進一步調用... – Venky

相關問題