使用 APIKEY 通过 windows powershell 访问 ASANA [英] Access ASANA via windows powershell using APIKEY
问题描述
我在 powershell 中创建了以下两个代码来访问 ASANA.
I have created this following two codes in powershell to access ASANA.
但它们都不起作用.我总是收到这个错误
But both of them dont work. I always get this error
远程服务器返回错误:(401) 未经授权."
"The remote server returned an error: (401) Unauthorized."
任何帮助表示赞赏.如果您有可用的 C# 代码,只需使用 APIKey 即可连接到任何安全服务器,请将其发布.我可以将其转换为 powershell 代码.
Any help appreciated. If you have a working C# code that can connect to any secure server with only APIKey, please post it. I could convert it to powershell code.
代码 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
推荐答案
(我在 Asana 工作)
(I work at Asana)
401 标头表明问题出在您的授权标头中.
The 401 header is a clue that the problem lies somewhere in your authorization header.
HTTP 基本身份验证规范不要求用户名:密码的 SHA1 哈希值.它只是该字符串的直接 base64 编码.尝试将 $authinfo
传递给您对 ToBase64String
的调用,而不是散列数据.
The HTTP Basic Auth spec does not call for a SHA1 hash of the username:password. It's just straight base64 encoding of that string. Try passing $authinfo
to your call to ToBase64String
instead of hashed data.
这篇关于使用 APIKEY 通过 windows powershell 访问 ASANA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!