hatch icon indicating copy to clipboard operation
hatch copied to clipboard

Error: API Token or Trusted Publisher must be used to upload in place of password

Open kolibril13 opened this issue 2 years ago • 42 comments

A few months ago, hatch publish worked fine for me. Now, I get the error:

jan-hendrik@Jan-Hendriks-MacBook-Air ipymafs % hatch publish
dist/ipymafs-0.1.2.tar.gz ... failed
Error uploading to repository: https://upload.pypi.org/legacy/ - Client error '401 User kolibril13 has two factor auth enabled,
an API Token or Trusted Publisher must be used to upload in place of password.' for url 'https://upload.pypi.org/legacy/'
For more information check: https://httpstatuses.com/401

I've already generated a token on pypi that looks like thispypi-AgEIdffx********************************************************* How can I feed this token to hatch, so that I can publish my packages? I'm grateful for any help!

kolibril13 avatar Oct 24 '23 15:10 kolibril13

Set the username to __TOKEN__

ofek avatar Oct 24 '23 16:10 ofek

Hi @ofek, I think I need more context: I can't find a reference how to set "username" in the docs.

I've also tried hatch publish --user pypi-AgEIdffx******************************************** and then put the token again in the field for Enter your credentials: but that did not work as well. image

Thanks for your help!

kolibril13 avatar Oct 24 '23 16:10 kolibril13

You are entering the token into the user field it appears

ofek avatar Oct 24 '23 18:10 ofek

thanks for the follow up-suggestion. I have now tried hatch publish --user but that gives Error: Option '--user' requires an argument.

I have then tried to hatch publish --user kolibril13, that opens this pop-up, but it there the pypi token does not work, only my system password is working there. image

If you have the bandwidth to give me more hints, I'd be very grateful for that!

kolibril13 avatar Oct 24 '23 19:10 kolibril13

Yes, Hatch uses your system's credential manager to store previous authentication info and that might require you entering your password. Did the upload work after that?

ofek avatar Oct 25 '23 03:10 ofek

ahh, now I got the solution! I had to type hatch publish --user __token__ and then in the second step Enter your credentials: I pasted the pypi-AgEIdffx**************************** token including the pypi- prefix.

Maybe this info can be added to https://hatch.pypa.io/latest/publish/#authentication

kolibril13 avatar Oct 25 '23 07:10 kolibril13

Follow-up question: For security reasons, I've deleted the old pypi-AgEIdffx******** and generated a new token on Pypi. How can I update this token now on hatch? Because when I now type hatch publish --user __token__ , I don't have the option Enter your credentials: anymore. Re-installation of hatch also did not work.

kolibril13 avatar Oct 25 '23 16:10 kolibril13

Use the flags of the publish command to set both the username and authentication info. The --initialize-auth flag might work also, let me know.

ofek avatar Oct 25 '23 20:10 ofek

Thanks for the hint. I've tried hatch publish --initialize-auth but that did not work. image

Here are the other options:

hatch publish -h               
Usage: hatch publish [OPTIONS] [ARTIFACTS]...

  Publish build artifacts.

Options:
  -r, --repo TEXT       The repository with which to publish artifacts [env
                        var: `HATCH_INDEX_REPO`]
  -u, --user TEXT       The user with which to authenticate [env var:
                        `HATCH_INDEX_USER`]
  -a, --auth TEXT       The credentials to use for authentication [env var:
                        `HATCH_INDEX_AUTH`]
  --ca-cert TEXT        The path to a CA bundle [env var:
                        `HATCH_INDEX_CA_CERT`]
  --client-cert TEXT    The path to a client certificate, optionally
                        containing the private key [env var:
                        `HATCH_INDEX_CLIENT_CERT`]
  --client-key TEXT     The path to the client certificate's private key [env
                        var: `HATCH_INDEX_CLIENT_KEY`]
  -n, --no-prompt       Disable prompts, such as for missing required fields
  --initialize-auth     Save first-time authentication information even if
                        nothing was published
  -p, --publisher TEXT  The publisher plugin to use (default is `index`) [env
                        var: `HATCH_PUBLISHER`]
  -o, --option TEXT     Options to pass to the publisher plugin. This may be
                        selected multiple times e.g. `-o foo=bar -o baz=23`
                        [env var: `HATCH_PUBLISHER_OPTIONS`]
  -y, --yes             Confirm without prompting when the plugin is disabled
  -h, --help            Show this message and exit.

which one of these can I try?

kolibril13 avatar Oct 25 '23 20:10 kolibril13

Use the flags of the publish command to set both the username and authentication info.

ofek avatar Oct 25 '23 20:10 ofek

I've now tried to use a few flags of the publish command to set both the username and authentication info, but that all did not work:

hatch publish --user __token__

-> ❌

hatch publish --initialize-auth

-> ❌

hatch publish --client-cert pypi-********

-> ❌

any further ideas how I can update the pypi token? Any suggestions are much appreciated!

kolibril13 avatar Oct 25 '23 21:10 kolibril13

Let's start from the top. To authenticate you need credentials and your username. Which flags do you think correspond to each one?

ofek avatar Oct 25 '23 22:10 ofek

I'm still a bit confused with the flags and their usage. I have tried --initialize-auth , --auth and --user but they all did not work. Could you possibly provide a step-by-step instruction or a direct command example for updating the token in hatch? I'm sure this would help me immensely in resolving the issue.

Thanks again for your support!

kolibril13 avatar Oct 26 '23 06:10 kolibril13

Try both the user and auth flags at the same time

ofek avatar Oct 26 '23 19:10 ofek

now I've tried

hatch publish  --auth --user
hatch publish  --user --auth
hatch publish  ---user __token__ --auth
hatch publish  --auth --user __token__
hatch publish  --user --auth __token__

but non of these worked. Is there a way to delete my old token and the corresponding Pypi-key on my machine?

kolibril13 avatar Oct 26 '23 21:10 kolibril13

hatch publish --user __TOKEN__ --auth this_is_your_token

ofek avatar Oct 26 '23 21:10 ofek

I appreciate your patience! 🌻 Unfortunately hatch publish --user __TOKEN__ --auth pypi-AgEIcH*********** gives me as well Client error '403 Invalid or non-existent authentication information.

Any other ideas?

kolibril13 avatar Oct 26 '23 21:10 kolibril13

Try to make a different token.

ofek avatar Oct 26 '23 23:10 ofek

I just generated a new token and tried that, but did not work as well.

kolibril13 avatar Oct 27 '23 17:10 kolibril13

Try hatch publish --user __token__ --auth this_is_your_token

ofek avatar Jan 03 '24 14:01 ofek

Hey @ofek , thanks for this hint! hatch publish --user __token__ --auth pypi-***************** is working now! 🎉

And I'd like to ask a follow-up question: Is there also a way to store the credentials, so that I don't have to always copy+paste my token?

kolibril13 avatar Jan 03 '24 15:01 kolibril13

I think it should be saved after it works once, try again?

ofek avatar Jan 03 '24 16:01 ofek

Thanks for the idea, but when I type just hatch publish, I still get this error:

image

kolibril13 avatar Jan 03 '24 16:01 kolibril13

Can you please try removing https://hatch.pypa.io/latest/config/hatch/#cache and do it again?

ofek avatar Jan 03 '24 16:01 ofek

After running rm -r ~/Library/Caches/hatch I now get this: Enter your username input field.

image but then comes this error message when I leave the `__TOKEN__` default.

kolibril13 avatar Jan 03 '24 16:01 kolibril13

Well yes but if you fix the username does it work I mean. Try again but this time fix the token variable (sorry about that)

ofek avatar Jan 03 '24 16:01 ofek

I see! When I use __token__ username in lowercase instead, I don't get the field Enter your credentials input field. Instead, I get directly the 403 Invalid or non-existent authentication information. message, as seen in this screenshot: image

For debugging, I tried to use another random username hellllowoooorld, and there I get the Enter your credentials input field.

So my suspicion is, that there's somewhere an old key cached that is not working anymore.

Do you have an idea where I could find and delete this old cached key?

kolibril13 avatar Jan 03 '24 17:01 kolibril13

Could you modify the code of your Hatch installation to add some print statements?

ofek avatar Jan 03 '24 19:01 ofek

I must admit that I don't know how that can be done. Can you guide me where I can find the code of the hatch installation, and where exactly I can add these print statemetents?

kolibril13 avatar Jan 03 '24 22:01 kolibril13

How did you install it?

ofek avatar Jan 03 '24 23:01 ofek