Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kolibril13 opened this issue Oct 24, 2023 · 43 comments

Comments

@kolibril13
Copy link

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!

@ofek
Copy link
Collaborator

ofek commented Oct 24, 2023

Set the username to __TOKEN__

@kolibril13
Copy link
Author

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!

@ofek
Copy link
Collaborator

ofek commented Oct 24, 2023

You are entering the token into the user field it appears

@kolibril13
Copy link
Author

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!

@ofek
Copy link
Collaborator

ofek commented Oct 25, 2023

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?

@kolibril13
Copy link
Author

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
Copy link
Author

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.

@ofek
Copy link
Collaborator

ofek commented Oct 25, 2023

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.

@kolibril13
Copy link
Author

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?

@ofek
Copy link
Collaborator

ofek commented Oct 25, 2023

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

@kolibril13
Copy link
Author

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!

@ofek
Copy link
Collaborator

ofek commented Oct 25, 2023

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

@kolibril13
Copy link
Author

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!

@ofek
Copy link
Collaborator

ofek commented Oct 26, 2023

Try both the user and auth flags at the same time

@kolibril13
Copy link
Author

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?

@ofek
Copy link
Collaborator

ofek commented Oct 26, 2023

hatch publish --user __TOKEN__ --auth this_is_your_token

@kolibril13
Copy link
Author

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?

@ofek
Copy link
Collaborator

ofek commented Oct 26, 2023

Try to make a different token.

@kolibril13
Copy link
Author

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

@ofek
Copy link
Collaborator

ofek commented Jan 3, 2024

Try hatch publish --user __token__ --auth this_is_your_token

@kolibril13
Copy link
Author

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?

@ofek
Copy link
Collaborator

ofek commented Jan 3, 2024

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

@kolibril13
Copy link
Author

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

image

@ofek
Copy link
Collaborator

ofek commented Jan 3, 2024

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

@kolibril13
Copy link
Author

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.

@ofek
Copy link
Collaborator

ofek commented Jan 3, 2024

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)

@kolibril13
Copy link
Author

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?

@ofek
Copy link
Collaborator

ofek commented Jan 3, 2024

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

@kolibril13
Copy link
Author

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?

@ofek
Copy link
Collaborator

ofek commented Jan 3, 2024

How did you install it?

@kolibril13
Copy link
Author

brew install hatch.
Is there maybe a command like hatch clear cache that deletes all previously stored api keys?

@ofek
Copy link
Collaborator

ofek commented Jan 4, 2024

You can find site-packages relative to the location shown:

hatch run :python -c "import sys;print(sys.executable)"

@kolibril13
Copy link
Author

jan-hendrik@JanHendriksAir ~ % hatch run :python3 -c "import sys;print(sys.executable)"
/opt/homebrew/opt/python@3.11/bin/python3.11
jan-hendrik@JanHendriksAir ~ % cd /opt/homebrew/opt/hatch 
jan-hendrik@JanHendriksAir hatch % ls -a
.			INSTALL_RECEIPT.json	libexec
..			LICENSE.txt		share
.DS_Store		README.md
.brew			bin
jan-hendrik@JanHendriksAir hatch % 

I got this.
What would be now a good next step?

@ofek
Copy link
Collaborator

ofek commented Jan 5, 2024

ls /opt/homebrew/opt/python@3.11
ls /opt/homebrew/opt/hatch/bin
/opt/homebrew/opt/python@3.11/bin/python3.11 -m site

@kolibril13
Copy link
Author

that gives me

jan-hendrik@JanHendriksAir ~ % ls /opt/homebrew/opt/python@3.11

Frameworks		Python Launcher 3.app	libexec
IDLE 3.app		README.rst		share
INSTALL_RECEIPT.json	bin
LICENSE			lib
jan-hendrik@JanHendriksAir ~ % ls /opt/homebrew/opt/hatch/bin

hatch
jan-hendrik@JanHendriksAir ~ % /opt/homebrew/opt/python@3.11/bin/python3.11 -m site
sys.path = [
    '/Users/jan-hendrik',
    '/opt/homebrew/Cellar/python@3.11/3.11.7/Frameworks/Python.framework/Versions/3.11/lib/python311.zip',
    '/opt/homebrew/Cellar/python@3.11/3.11.7/Frameworks/Python.framework/Versions/3.11/lib/python3.11',
    '/opt/homebrew/Cellar/python@3.11/3.11.7/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload',
    '/opt/homebrew/lib/python3.11/site-packages',
    '/opt/homebrew/Cellar/pybind11/2.11.1/libexec/lib/python3.11/site-packages',
    '/Users/jan-hendrik/projects/ipyreact',
    '/opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages',
]
USER_BASE: '/Users/jan-hendrik/Library/Python/3.11' (doesn't exist)
USER_SITE: '/Users/jan-hendrik/Library/Python/3.11/lib/python/site-packages' (doesn't exist)
ENABLE_USER_SITE: True

@ofek
Copy link
Collaborator

ofek commented Jan 5, 2024

Should be a hatch directory in one of the following:

  • /opt/homebrew/lib/python3.11/site-packages
  • /opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages

@kolibril13
Copy link
Author

both exists:

jan-hendrik@Jan-Hendriks-MacBook-Air ~ % cd /opt/homebrew/lib/python3.11/site-packages/hatchling && ls -a

.		__main__.py	cli		ouroboros.py	version
..		bridge		dep		plugin
__about__.py	build.py	licenses	py.typed
__init__.py	builders	metadata	utils
jan-hendrik@Jan-Hendriks-MacBook-Air hatchling % cd /opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/hatchling && ls -a

.		__main__.py	cli		ouroboros.py	version
..		bridge		dep		plugin
__about__.py	build.py	licenses	py.typed
__init__.py	builders	metadata	utils

@ofek
Copy link
Collaborator

ofek commented Jan 5, 2024

Look for hatch rather than hatchling. After you find it try to modify code to see what happens (I'm assuming one of those directories is actually a symbolic link to the other)

@kolibril13
Copy link
Author

I cannot find a hatch folder there:

jan-hendrik@Jan-Hendriks-MacBook-Air ~ % cd /opt/homebrew/lib/python3.11/site-packages/hatch && ls -a 

cd: no such file or directory: /opt/homebrew/lib/python3.11/site-packages/hatch
jan-hendrik@Jan-Hendriks-MacBook-Air ~ % cd /opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/hatch && ls -a
 
cd: no such file or directory: /opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/hatch
jan-hendrik@Jan-Hendriks-MacBook-Air ~ % 

@ofek
Copy link
Collaborator

ofek commented Jan 5, 2024

/opt/homebrew/opt/python@3.11/bin/python3.11 -c "import hatch;print(hatch)"

@kolibril13
Copy link
Author

jan-hendrik@Jan-Hendriks-MacBook-Air ~ % /opt/homebrew/opt/python@3.11/bin/python3.11 -c "import hatch;print(hatch)"                                       

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'hatch'

@z3z1ma
Copy link

z3z1ma commented Jan 13, 2024

Recommend fzf when searching for files
https://github.com/junegunn/fzf

Also consider if you can just do hatch --version
Then python -m venv hatch_test && source hatch_test/bin/activate
The modify files in the venv there faster for your debugging

image

@kolibril13
Copy link
Author

I've just reset my whole Mac and then re-installed the latest version of hatch, now the problem is gone 🎉
Thanks again for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants