通过使用API​​KEY的Windows PowerShell访问ASANA(Access ASAN

2019-09-20 16:23发布

我已经创建了PowerShell中以下两个代码访问ASANA。

但他们两人不工作。 我总是得到这个错误

“远程服务器返回错误:(401)未授权”

任何帮助表示赞赏。 如果你有一个工作的C#代码只能与APIKey连接到任何安全服务器,请张贴。 我可以将其转换为PowerShell代码。

代码1

Function Get-WebResponseString
{
    param (
        [Parameter(Mandatory=$true)]
        [String]$Url,       
        [Parameter(Mandatory=$true)]
        [String]$Method,
        [Parameter(Mandatory=$false)]
        [System.Net.NetworkCredential]$Credential
    )

    $Request = [System.Net.WebRequest]::Create($Url)    
    $Request.Method = $Method
    $Request.ContentType = "application/x-www-form-urlencoded";
    if ($Credential -ne $null)
    {
        $Request.Credentials = $credential
        write-host "****" -foregroundcolor blue
    }

    $Response = $Request.GetResponse()

    $StreamReader = New-Object System.IO.StreamReader $Response.GetResponseStream()
    $StreamReader.ReadToEnd()
} 

$Url = "https://app.asana.com/api/1.0/tasks"
$Username = "MMuthusamy@xxxxxx.xom"
$apikey="xxxxxxxxx"

$credential = New-Object System.Net.NetworkCredential @($Username, $apikey)   

Get-WebResponseString -Url $Url -Credential $credential -Method "GET" 

码2

$sha = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider 

$apikey="xxxxxxx"
#Add colon
$authinfo=$apikey+":";
$string1 = $authinfo
Write-Host $string1  -ForeGroundColor Green

#Encoding format
$enc = [system.Text.Encoding]::UTF8

#get bytes
$data1 = $enc.GetBytes($string1) 

#Encode
$result1 = $sha.ComputeHash($data1)

#convert to 64 bit
$mykey=[System.Convert]::ToBase64String($result1)

Write-Host $mykey -ForeGroundColor Green

$url = "https://app.asana.com/api/1.0/tasks"
$url="https://app.asana.com/api/1.0/users"
$request = [System.Net.WebRequest]::Create($url)
$authorization = "Authorization: Basic " + $myKey
Write-Host $authorization -ForeGroundColor Green

$request.Headers.Add($authorization)
#$request.Headers.Add("Authorization: BASIC $mykey")
$response = $request.GetResponse()
Write-Host $Response  -ForeGroundColor Green 

Answer 1:

(我在体位工作)

401头的是,问题出在你的授权头某处的线索。

在HTTP基本认证规范不要求用户名的SHA1哈希:密码。 这是该字符串的只是直接base64编码。 尝试通过$authinfo您来电ToBase64String而不是散列数据。



文章来源: Access ASANA via windows powershell using APIKEY
标签: api-key asana