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

[bug]: generate_secured_api_key fails on Python versions > 3.12, dict and None objects do not have the encode method #576

Open
bsmith8457 opened this issue Dec 24, 2024 · 0 comments

Comments

@bsmith8457
Copy link

bsmith8457 commented Dec 24, 2024

Description

This code:

apiKey = str(current_app.config['ALGOLIA_API_KEY'])
restrictions = dict({
    'userToken': str(userId)
})
key = SearchClientSync.generate_secured_api_key(apiKey,restrictions)

Fails with the error Error Message: 'dict' object has no attribute 'encode'

Which, when looking in your code I notice:

secured_key = hmac.new(
    parent_api_key.encode(encoding="utf-8"),
    query_parameters.encode(encoding="utf-8"),
    hashlib.sha256,
).hexdigest()

Where it's expecting a dict object to have the encode method, which it does not.

This is your API documentation:

public_key = SearchClient.generate_secured_api_key(
    'YourSearchOnlyApiKey',
    {'userToken': 'user_42'}
)

Note that when I switch this argument to a string, it works, supposedly:

apiKey = str(current_app.config['ALGOLIA_API_KEY'])
restrictions = str({
    'userToken': str(userId)
})
key = SearchClientSync.generate_secured_api_key(apiKey,restrictions)

The documentation is pretty misleading or out of date.

Client

Search

Version

4.11.2

Relevant log output

No response

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

1 participant