HTTP Basic Auth request?

Home Forums PowerShell Q&A HTTP Basic Auth request?

You must be registered and logged in to reply to this topic.

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 2 years, 6 months ago.

  • #5917

    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 http://www.getharvest.com/api/time-tracking I am not able to authenitcate from PowerShell.

    I have tried using the "old" way

    <br /><br /><br />$user = &quot;username&quot;<br />$pass = &quot;password&quot;<br /><br />$url = &quot;https&#58;//mydomain&#46;harvestapp&#46;com/account/who_am_i&quot;<br />$auth = 'Basic ' + (ConvertTo-Base64 -string &quot;$user\&#58;$pass&quot;)<br /><br />$req = New-Object System&#46;Net&#46;WebClient<br />$req&#46;Headers&#46;Add('Authorization', $auth )<br /><br />&#91;xml&#93;$webpage = $req&#46;DownloadString($url)<br /><br />$webpage

    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 https&#58;//yooakim&#46;harvestapp&#46;com/account/who_am_i -Headers @{&quot;AUTHORIZATION&quot;=&quot;Basic base64encodedtoken=&quot;} -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 http://fiddler2.com/fiddler2/ 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:
    <br />Invoke-WebRequest -Uri $baseuri -Headers @{&quot;Authorization&quot; = &quot;Basic &quot;+&#91;System&#46;Convert&#93;&#58;&#58;ToBase64String(&#91;System&#46;Text&#46;Encoding&#93;&#58;&#58;UTF8&#46;GetBytes($username+&quot;&#58;&quot;+$password ))} -Method Get<br />

    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.

    Cheers,
    Joakim

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

    I got it working, this way:

    param(<br />    &#91;string&#93;$account = 'acctname',<br />    &#91;string&#93;$username = 'userid',<br />    &#91;string&#93;$password = 'password',<br />    &#91;int&#93;$day = (Get-Date)&#46;DayOfYear,<br />    &#91;int&#93;$year = (get-date)&#46;Year<br />)<br />$uri = &quot;https&#58;//$account&#46;harvestapp&#46;com/daily/$day/$year&quot;<br />$auth = 'Basic ' + &#91;System&#46;Convert&#93;&#58;&#58;ToBase64String(&#91;System&#46;Text&#46;Encoding&#93;&#58;&#58;UTF8&#46;GetBytes($username+&quot;&#58;&quot;+$password ))<br />$req = New-Object System&#46;Net&#46;WebClient<br />$req&#46;Headers&#46;Add('Content-Type', 'application/xml')<br />$req&#46;Headers&#46;Add('Accept', 'application/xml')<br />$req&#46;Headers&#46;Add('Authorization', $auth )<br /><br />&#91;xml&#93;$webpage = $req&#46;DownloadString($uri)

You must be registered and logged in to reply to this topic.