1

I have a zip file hosted at a URL. I'm writing a Powershell script to get that zip file using Invoke-RestMethod and save it to disk. When I run the Powershell script, the zip file is downloaded and saved to disk, however, I cannot open or extract it. Does anyone know what I'm doing wrong? Here is my code that handles the zip download/save. Here is my code (I've changed the URL for the zip for security reasons. If I type in the actual URL into a browser it will be downloaded by the browser after which I can locate it on disk and extract it).

$username = "uname"
$password = "pword"

$boundary = [guid]::NewGuid().ToString().Replace("-", "").Substring(0, 16)
$enc = [System.Text.Encoding]::GetEncoding("utf-8")
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

$headers = @{}
$headers.Add("Host", "api.bitbucket.org")
$headers.Add("Authorization", "Basic $base64AuthInfo")

$repoResult
    $repoResult = Invoke-WebRequest -Uri "https://bitbucket.org/teamAccount/repoName/get/master.zip" -Headers $headers -Method GET -Verbose -OutFile .\output.zip 
}

EDIT/UPDATE: Thanks to TesselatedHeckler's comment, I've discovered that the downloaded zip is actually an HTML login page. I also realized that I didn't have my headers (which contains the login info) in Invoke-RestMethod line. I've updated the post and my script. Now, whenever I run my script, I get a (500) Internal Server Error.

Roka545
  • 3,404
  • 20
  • 62
  • 106
  • 1
    So what's in the file downloaded by the code? Is it the right size? Open it in a text editor, is it some HTML redirect/login prompt? – TessellatingHeckler Aug 10 '17 at 17:32
  • @TessellatingHeckler Didn't even think about doing that - it is indeed an HTML file with a login prompt. – Roka545 Aug 10 '17 at 17:41
  • Didn't even realize I commented out the headers. I've updated the post. Now when running the script I get a (500) Internal Server Error. – Roka545 Aug 10 '17 at 17:43
  • I urge you to read the documentation of the Bitbucket API. A quick look showed me that you are using the wrong url. Api requests should point to https://api.bitbucket.org/ for starters. – bluuf Aug 10 '17 at 18:04
  • @bluuf I guess I was technically not doing an actual Bitbucket API call, but rather just trying to download from a URL. I was trying to follow this post: https://bitbucket.org/site/master/issues/6548/download-tar-zip-with-oauth-api-token – Roka545 Aug 10 '17 at 18:07

0 Answers0