cthoyt / zenodo-client

A tool for automated uploading and version management of scientific data to Zenodo
MIT License
25 stars 5 forks source link

Confusing documentation related to `key` #7

Closed sgbaird closed 1 year ago

sgbaird commented 1 year ago

Running the main example:

from zenodo_client import Creator, Metadata, ensure_zenodo
from my_secrets import ZENODO_API_KEY, ZENODO_SANDBOX_API_KEY

# Define the metadata that will be used on initial upload
data = Metadata(
    title='Test Upload 3',
    upload_type='dataset',
    description='test description',
    creators=[
        Creator(
            name='Hoyt, Charles Tapley',
            affiliation='Harvard Medical School',
            orcid='0000-0003-4423-4370',
        ),
    ],
)

sandbox = True
key = ZENODO_SANDBOX_API_KEY if sandbox else ZENODO_API_KEY
res = ensure_zenodo(
    key,
    data=data,
    paths=[
        "data/processed/particle_packing/sobol_probability_filter.csv",
        # "data/processed/particle_packing/sobol_regression.csv",
    ],
    sandbox=sandbox,  # remove this when you're ready to upload to real Zenodo
)
from pprint import pprint

pprint(res.json())
Exception has occurred: HTTPError       (note: full exception trace is shown but execution is paused at: _run_module_as_main)
401 Client Error: UNAUTHORIZED for url: https://sandbox.zenodo.org/api/deposit/depositions
  File "C:\Users\sterg\miniconda3\envs\matsciopt\Lib\site-packages\requests\models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
  File "C:\Users\sterg\miniconda3\envs\matsciopt\Lib\site-packages\zenodo_client\api.py", line 112, in create
    res.raise_for_status()
  File "C:\Users\sterg\miniconda3\envs\matsciopt\Lib\site-packages\zenodo_client\api.py", line 50, in create_zenodo
    return Zenodo(**kwargs).create(data, paths)
  File "C:\Users\sterg\miniconda3\envs\matsciopt\Lib\site-packages\zenodo_client\api.py", line 41, in ensure_zenodo
    res = create_zenodo(data, paths, **kwargs)
  File "C:\Users\sterg\Documents\GitHub\sparks-baird\matsci-opt-benchmarks\scripts\particle_packing\zenodo_upload.py", line 20, in <module>
    res = ensure_zenodo(
  File "C:\Users\sterg\miniconda3\envs\matsciopt\Lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\sterg\miniconda3\envs\matsciopt\Lib\runpy.py", line 197, in _run_module_as_main (Current frame)
    return _run_code(code, main_globals, None,

Adding access_token=key fixes this:

from zenodo_client import Creator, Metadata, ensure_zenodo
from my_secrets import ZENODO_API_KEY, ZENODO_SANDBOX_API_KEY

# Define the metadata that will be used on initial upload
data = Metadata(
    title='Test Upload 3',
    upload_type='dataset',
    description='test description',
    creators=[
        Creator(
            name='Hoyt, Charles Tapley',
            affiliation='Harvard Medical School',
            orcid='0000-0003-4423-4370',
        ),
    ],
)

sandbox = True
key = ZENODO_SANDBOX_API_KEY if sandbox else ZENODO_API_KEY
res = ensure_zenodo(
    key,
    data=data,
    paths=[
        "data/processed/particle_packing/sobol_probability_filter.csv",
        # "data/processed/particle_packing/sobol_regression.csv",
    ],
    sandbox=sandbox,  # remove this when you're ready to upload to real Zenodo
    access_token=key, # NEEDED TO ADD THIS
)
from pprint import pprint

pprint(res.json())
sgbaird commented 1 year ago

I'm realizing I was misunderstanding key. key is some unique name of your choosing that gets connected to a deposition ID. access_token is the API key that needs to get passed via the kwarg as above. I think this could be made clearer in the documentation.

cthoyt commented 1 year ago

@sgbaird Thank you for point this out. I added a commend in https://github.com/cthoyt/zenodo-client/commit/782e0e24274beb6f82ea393021e0450cc04aec63 that is hopefully sufficient. If not, please feel free to send a PR with more text.

Thank you so much for the feedback on this repo and on pystow!