使用API​​KEY通过Windows Powershell访问ASANA [英] Access ASANA via windows powershell using APIKEY

查看:107
本文介绍了使用API​​KEY通过Windows Powershell访问ASANA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在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#代码,仅使用API​​Key即可连接到任何安全服务器,请发布该代码.我可以将其转换为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.

这篇关于使用API​​KEY通过Windows Powershell访问ASANA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆