gbif / gbif-api

GBIF API
Apache License 2.0
27 stars 5 forks source link

'User' not allowed to create download with creator #107

Closed simlal closed 11 months ago

simlal commented 11 months ago

Hello,

I am trying to use the /occurrence/download/request API endpoint to create a new download file for a given set of predicates (query parameters). I do have a valid username, account and think in my download request's code using the 'request' lib:

def request_download_gbif_mascarocoffea_entries(
    email: str,
    reponse_format: str = "DWCA",
    year_range: Optional[tuple[int]] = None,
) -> Union[requests.Response, dict]:

    # HTTP-based auth for post.req
    gbif_username = os.getenv("GBIF_USERNAME")
    gbif_password = os.getenv("GBIF_PASSWORD")

    if not all([isinstance(param, str) for param in [reponse_format, email]]):
        raise TypeError("'response_format' and 'email' must be of type str.")

    possible_formats = ["SIMPLE_CSV", "DWCA", "SPECIES_LIST"]
    if reponse_format not in possible_formats:
        raise ValueError(f"'reponse_format' must be one of {possible_formats}")

    if year_range is not None:
        if not isinstance(year_range, tuple):
            raise TypeError("'year_range' must be a tuple")
        if len(year_range) != 2:
            raise ValueError("'year_range' must contain exactly 2 elements.")
        if not all([isinstance(year, int) for year in year_range]):
            raise TypeError("all elements of year range must be integers")
        if year_range[0] > year_range[1]:
            raise ValueError("First year element in range must be smaller than the second.")

    # Query parameters for payload specific to mascarocoffea with valid coordinates
    url = "https://api.gbif.org/v1/occurrence/download/request"
    predicates = [
        {"type": "equals", "key": "COUNTRY", "value": "MG"},
        {"type": "equals", "key": "HAS_COORDINATE", "value": "true"},
        {"type": "equals", "key": "HAS_GEOSPATIAL_ISSUE", "value": "false"},
        {"type": "equals", "key": "TAXON_KEY", "value": "2895315"},
        {"type": "equals", "key": "OCCURRENCE_STATUS", "value": "present"}
    ]

    if year_range is not None:    # Defaults to all occurrences accross all years
        predicates.append({"type": "equals", "key": "YEAR", "value": f"{year_range[0]},{year_range[1]}"})

    payload = {
        "creator": [gbif_username],
        "notificationAddresses": [email],
        "predicate": {
            "type": "and",
            "predicates": predicates
        },
        "format": "SIMPLE_CSV"
    }

    try:
        response = requests.post(url=url, json=payload, auth=(gbif_username, gbif_password))
        data = response.json()
        return data
    except requests.HTTPError:
        print(f"Request failed with status code {response.status_code}")
        return response
    except ValueError:
        print(f"Cannot decode response: {response.text}")
        return response

And when calling the function:

data = request_download_gbif_mascarocoffea_entries("simlalonde@hotmail.com", year_range=(2010, 2011))
>>> Cannot decode response: simlal not allowed to create download with creator 

Posting on GitHub because somehow I am blocked from helpdesk@gbif.org.

Thanks! Simon

MattBlissett commented 11 months ago
        "creator": [gbif_username],

Is this sending an array with a single string? It should just be a string: "creator": "simlal".

simlal commented 11 months ago

riiiiiiiight silly mistake much appreciated :)