401 unauthorized when pushing an image layer to dockerhub

I have been trying to use curl cmd to push an image layer to my repository on dockerhub here https://hub.docker.com/r/patiner/busybox/. In particular, I want to obtain a url from the registry to upload the layer via POST /v2/<name>/blobs/uploads/.

Here are my steps:

Step 1. curl -u patiner:pwd -X POST -kv https://registry-1.docker.io/v2/patiner/busybox/blobs/uploads/

Step 2. Now after the above cmd, I get “401 unauthorized” and the header of the response contains Www-Authenticate: Bearer realm="https://auth.docker.io/token",service="registry.docker.io",scope="repository:patiner/busybox:pull,push"

Step 3. Obtain the token by curl -u patiner:pwd -kv https://auth.docker.io/token?scope=repository%3Apatiner%2Fbusybox%3Apull%2Cpush%22&service=registry.docker.io

Step 4. Obtain the upload url by curl -u patiner:pwd -X POST -H 'Authorization Bearer ${token}' -kv https://registry-1.docker.io/v2/patiner/busybox/blobs/uploads/

However, after step 4, I still get the same error response:

< HTTP/1.1 401 Unauthorized
< Content-Type: application/json; charset=utf-8
< Docker-Distribution-Api-Version: registry/2.0
< Www-Authenticate: Bearer realm="https://auth.docker.io/token",service="registry.docker.io",scope="repository:patiner/busybox:pull,push"
< Date: Tue, 11 Jul 2017 14:24:06 GMT
< Content-Length: 232
< Strict-Transport-Security: max-age=31536000
{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":[{"Type":"repository","Class":"","Name":"patiner/busybox","Action":"pull"},{"Type":"repository","Class":"","Name":"patiner/busybox","Action":"push"}]}]}

Could someone help me figure out what the problem might be? Thanks.

So I found this https://gist.github.com/jlhawn/8f218e7c0b14c941c41f and it did work for me. The only trick is that I cannot use curl -u user:pwd; instead, it has to be base64 encoded. I don’t know the reason for doing this.