HTTP Basic Auth request?

1 post

by yooakim at 2012-09-06 12:34:24

I am trying to acce the Harvest API using Basic HTTP authentication. I have an account with Harvest that works fine.

When I try to use PowerShell to use the Time Tracking API I am not able to authenitcate from PowerShell.

I have tried using the "old" way

$user = "username"
$pass = "password"

$url = ""
$auth = 'Basic ' + (ConvertTo-Base64 -string "$user\:$pass")

$req = New-Object System.Net.WebClient
$req.Headers.Add('Authorization', $auth )

[xml]$webpage = $req.DownloadString($url)


This only gives me a 401 Not Authorized. The username/password is verified to be correct.

I have also tried the Invoke-WebRequest and Invoke-RestMethod, same result. I tried this:

Invoke-RestMethod -Uri -Headers @{"AUTHORIZATION"="Basic base64encodedtoken="} -Method Get

Can anyone help me get the HTTP Basic Authenitcation correct?

by DonJ at 2012-09-06 15:03:34

You need to see what else your browser is sending in its HTTP request header. It's likely there's some cookie or something that needs to be set. Remember, PowerShell isn't a Web browser, so you'll have to manually do whatever the browser is normally doing automatically.

by Eric at 2012-09-13 22:48:57

Download and capture the transaction from your browser and check the fiddler output. As Don says there could be some underlying object your not accounting for in the GET request.

by nykolla at 2012-09-15 11:34:49

I think this will work for you:

Invoke-WebRequest -Uri $baseuri -Headers @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+":"+$password ))} -Method Get

by yooakim at 2012-10-05 06:00:03

Thanks for the suggestions but for some weird reason the Auth header does not seem to be sent with the request? When I view the session using Fiddler the request does not containt the auth header, Fiddler says:

No Proxy-Authenticate Header is present.
No WWW-Authenticate Header is present.

I'm doing this on a Windows 8 RTM machine.


by yooakim at 2012-10-05 10:39:02

I got it working, this way:

[string]$account = 'acctname',
[string]$username = 'userid',
[string]$password = 'password',
[int]$day = (Get-Date).DayOfYear,
[int]$year = (get-date).Year
$uri = "https://$$day/$year"
$auth = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+":"+$password ))
$req = New-Object System.Net.WebClient
$req.Headers.Add('Content-Type', 'application/xml')
$req.Headers.Add('Accept', 'application/xml')
$req.Headers.Add('Authorization', $auth )

[xml]$webpage = $req.DownloadString($uri)

You must be logged in to reply in this thread.