0

I tried following the post to access the asana api found here Access ASANA via windows powershell using APIKEY . Below is the code I'm using and the error message I receive:

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

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

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

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

Write-Host $mykey -ForeGroundColor Green


$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 

and receive the following error:

Exception calling "GetResponse" with "0" argument(s): "The operation has timed out" At line:26 char:1 + $response = $request.GetResponse() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException

Community
  • 1
  • 1
Sean C.
  • 23
  • 5
  • 1
    The url and auth header looks ok to me. Even if you made a bad request you shouldn't get a timeout. I've unfamiliar with powershell - do you have to do something special to make HTTPS requests? Another thing to try is a different request, i.e. try `/users/me` instead of `/users` and see if it also times out. – Greg S Aug 04 '15 at 02:46
  • I agree with @GregS. It looks good so far, and a timeout means just that; no response at all. That's not typical of a web application that is working correctly. Nothing special is required for HTTPS unless the certificate has issues (expired, host/CN mismatch, etc.). I don't know which version of PowerShell you're using, but you can use `Invoke-WebRequest` or `Invoke-RestMethod` in PowerShell 3 and higher. They're a bit easier to use than `[System.Net.WebRequest]`, but I doubt it will help with your problem. – briantist Aug 04 '15 at 02:53
  • Thanks, I've tried `Invoke-RestMethod` as well as `Invoke-WebRequest` without any additional luck. I'm still receiving the same error as before. – Sean C. Aug 04 '15 at 18:30

2 Answers2

0

Try a different request - there is a known issue with Asana API requests that cross workspace/organization boundaries where they could traverse too much data and timeout (they are sharded and there is no way to paginate these efficiently so we are planning on deprecating them).

The /users endpoint without any params will fetch all users across all workspaces you're in and may have this problem. A simpler call like /users/me or /tasks/KNOWN-TASK-ID will tell you if you've got the basics right or not.

Greg S
  • 2,079
  • 1
  • 11
  • 10
  • Thank you for helping troubleshoot this issue. I changed the URL to be https://app.asana.com/api/1.0/users/me and still receive the same error message about the operation timing out when calling GetResponse with 0 arguments. – Sean C. Aug 04 '15 at 22:19
  • Ok. Then I'm more certain it's some kind of request setup or connectivity error and not an issue with the API or the service. Are you able to use `curl` to make requests manually? There are lots of things that could go wrong between your library and the Asana servers, it sounds like you'll have to keep experimenting with things to narrow down where the problem is. – Greg S Aug 04 '15 at 22:23
  • So I ran a `curl https://app.asana.com/api/1.0/users/me` and the operation times out. I was expecting it to fail due to authentication but it simply timed out. – Sean C. Aug 04 '15 at 23:10
  • At this point you're going to need to do some kind of network troubleshooting to determine why you cannot reach the Asana server. There is nothing indicating an Asana- or API-specific nature to your problem. If you're unsure of how to troubleshoot from here, there are probably further resources on the web, but this is a very useful/important thing to learn as a web developer. Good luck! – Greg S Aug 05 '15 at 00:09
0

I ended up just using GOW to run the curl command. The reason my cURL was failing initially was because of the ssl certficiate. I simply added the --insecure switch and that resolved the issue. Here is the final piece of code for anyone else looking:

$api_key = "********.******************"
$command = CMD /C "curl --insecure --user ${api_key}: https://app.asana.com/api/1.0/users"
Sean C.
  • 23
  • 5