【问题标题】:Access ASANA via windows powershell using APIKEY使用 APIKEY 通过 windows powershell 访问 ASANA
【发布时间】:2012-08-02 16:37:09
【问题描述】:

我在 powershell 中创建了以下两个代码来访问 ASANA。

但它们都不起作用。我总是收到这个错误

“远程服务器返回错误:(401) Unauthorized.”

任何帮助表示赞赏。如果您有一个可以使用 APIKey 连接到任何安全服务器的工作 C# 代码,请发布它。我可以将其转换为 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 

【问题讨论】:

    标签: api-key asana


    【解决方案1】:

    (我在 Asana 工作)

    401 标头表明问题出在您的授权标头中。

    HTTP 基本身份验证规范不要求用户名:密码的 SHA1 哈希。它只是该字符串的直接base64编码。尝试将 $authinfo 传递给您对 ToBase64String 的调用,而不是散列数据。

    【讨论】:

    • 太棒了。谢谢。像冠军一样工作。这是我更新的代码 $apikey="*********************" $authinfo=$apikey+":"; $enc = [system.Text.Encoding]::UTF8 $data1 = $enc.GetBytes($authinfo) $mykey=[System.Convert]::ToBase64String($data1) $url="app.asana.com/api/1.0/users" $request = [System.Net.WebRequest]::Create($url) $authorization = "授权:基本" + $myKey $request.Headers.Add($authorization) $Response = $Request.GetResponse() $StreamReader = New-Object System.IO.StreamReader $Response.GetResponseStream() $StreamReader.ReadToEnd()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 2017-10-07
    相关资源
    最近更新 更多