   isAtomShellVersionCached = (version) ->
     grunt.file.isFile(getCachePath(version), 'version')
 
-  downloadAtomShell = (version, callback) ->
+  downloadAtomShell = (version, url, callback) ->
     options =
-      url: "https://gh-contractor-zcbenz.s3.amazonaws.com/atom-shell/#{version}/atom-shell-#{version}-darwin.zip"
+      url: url
+      followRedirect: false
       proxy: process.env.http_proxy || process.env.https_proxy
+
+    # Only set headers for GitHub host, the url could also be a S3 link and
+    # setting headers for it would make the request fail.
+    if require('url').parse(url).hostname is 'api.github.com'
+      options.headers =
+        authorization: "token #{getTokenFromKeychain()}"
+        accept: 'application/octet-stream'
 
     inputStream = request(options)
     inputStream.on 'response', (response) ->
-      if response.statusCode is 200
+      if response.statusCode is 302
+        # Manually handle redirection so headers would not be sent for S3.
+        downloadAtomShell(version, response.headers.location, callback)
+      else if response.statusCode is 200
         grunt.log.writeln("Downloading atom-shell version #{version.cyan}")
         cacheDirectory = getCachePath(version)
         rm(cacheDirectory)


module.exports = (grunt) ->
  {spawn, mkdir, rm, cp} = require('./task-helpers')(grunt)

  getAtomShellVersion = ->
    versionPath = path.join('atom-shell', 'version')

          installAtomShell(atomShellVersion)
          rebuildNativeModules(currentAtomShellVersion, done)
        else
          downloadAtomShellOfVersion atomShellVersion, (error, zipPath) ->
            if zipPath?
              unzipAtomShell zipPath, (error) ->
                if error?
