DieterB
DieterB

Reputation: 31

IBM text to speech Python DecodeError

I just tried the basic example of IBM's text to speech with Python:

!pip install ibm_watson

from ibm_watson import TextToSpeechV1

from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

apikey = 'my API KEY'
url = 'my SERVICE URL'

authenticator = IAMAuthenticator(apikey)
tts = TextToSpeechV1(authenticator=authenticator)
tts.set_service_url(url)

with open('./speech.mp3', 'wb') as audio_file:
    res = tts.synthesize('Hello World!', accept='audio/mp3', voice='en-US_AllisonV3Voice').get_result()
    audio_file.write(res.content)

But I get an error message: DecodeError: It is required that you pass in a value for the "algorithms" argument when calling decode().*

DecodeError                               Traceback (most recent call last)
<ipython-input-5-53b9d398591b> in <module>
      1 with open('./speech.mp3', 'wb') as audio_file:
----> 2     res = tts.synthesize('Hello World!', accept='audio/mp3', voice='en-US_AllisonV3Voice').get_result()
      3     audio_file.write(res.content)

c:\users\chris\appdata\local\programs\python\python39\lib\site-packages\ibm_watson\text_to_speech_v1.py in synthesize(self, text, accept, voice, customization_id, **kwargs)
    275 
    276         url = '/v1/synthesize'
--> 277         request = self.prepare_request(method='POST',
    278                                        url=url,
    279                                        headers=headers,

c:\users\chris\appdata\local\programs\python\python39\lib\site-packages\ibm_cloud_sdk_core\base_service.py in prepare_request(self, method, url, headers, params, data, files, **kwargs)
    295         request['data'] = data
    296 
--> 297         self.authenticator.authenticate(request)
    298 
    299         # Next, we need to process the 'files' argument to try to fill in

c:\users\chris\appdata\local\programs\python\python39\lib\site-packages\ibm_cloud_sdk_core\authenticators\iam_authenticator.py in authenticate(self, req)
    104         """
    105         headers = req.get('headers')
--> 106         bearer_token = self.token_manager.get_token()
    107         headers['Authorization'] = 'Bearer {0}'.format(bearer_token)
    108 

c:\users\chris\appdata\local\programs\python\python39\lib\site-packages\ibm_cloud_sdk_core\jwt_token_manager.py in get_token(self)
     77         """
     78         if self._is_token_expired():
---> 79             self.paced_request_token()
     80 
     81         if self._token_needs_refresh():

c:\users\chris\appdata\local\programs\python\python39\lib\site-packages\ibm_cloud_sdk_core\jwt_token_manager.py in paced_request_token(self)
    122             if not request_active:
    123                 token_response = self.request_token()
--> 124                 self._save_token_info(token_response)
    125                 self.request_time = 0
    126                 return


c:\users\chris\appdata\local\programs\python\python39\lib\site-packages\ibm_cloud_sdk_core\jwt_token_manager.py in _save_token_info(self, token_response)
    189 
    190         # The time of expiration is found by decoding the JWT access token
--> 191         decoded_response = jwt.decode(access_token, verify=False)
    192         # exp is the time of expire and iat is the time of token retrieval
    193         exp = decoded_response.get('exp')

c:\users\chris\appdata\local\programs\python\python39\lib\site-packages\jwt\api_jwt.py in decode(self, jwt, key, algorithms, options, **kwargs)
    111         **kwargs,
    112     ) -> Dict[str, Any]:
--> 113         decoded = self.decode_complete(jwt, key, algorithms, options, **kwargs)
    114         return decoded["payload"]
    115 

c:\users\chris\appdata\local\programs\python\python39\lib\site-packages\jwt\api_jwt.py in decode_complete(self, jwt, key, algorithms, options, **kwargs)
     77 
     78         if options["verify_signature"] and not algorithms:
---> 79             raise DecodeError(
     80                 'It is required that you pass in a value for the "algorithms" argument when calling decode().'
     81             )

DecodeError: It is required that you pass in a value for the "algorithms" argument when calling decode().

Upvotes: 3

Views: 3071

Answers (6)

Jorge Agull&#243;
Jorge Agull&#243;

Reputation: 1

I had this problem, with version 2.3.0 of jwt.

I have solved it like this:

jwt.decode(token, MY_SECRET, algorithms=['HS256'])

you must use algorithms instead of algorithm

Upvotes: 0

Praveen
Praveen

Reputation: 1881

I was having this problem with PyJWT==2.1.0,

My existing code,

import jwt

data = 'your_data'
key  = 'your_secret_key' 

encoded_data = jwt.encode(data, key) # worked
decoded_data = jwt.decode(encoded_data, key) # did not worked

I passed the algorithm for encoding and afterwards it worked fine, Solution:

import jwt

data = 'your_data'
key  = 'your_secret_key' 

encoded_data = jwt.encode(data, key, algorithm="HS256")
decoded_data = jwt.decode(encoded_data, key, algorithms=["HS256"])

More information is available in JWT Docs

Upvotes: 1

Gaurav Jain
Gaurav Jain

Reputation: 1865

For those who want to use the latest version(as of writing this v2.1.0) of PyJWT.

If you don't want to continue with the older version(i.e PyJWT==1.7.1) and want to upgrade it for some reason, you need to use the verify_signature parameter and set it to False(It is True by default if you don't provide it). In older versions (before <2.0.0) the parameter was verify and you could directly use that. but in the newer version, you have to use it inside options parameter which is of type dict

jwt.decode(...., options={"verify_signature": False})

If you don't want to use verify_signature, you can simply pass the algorithms parameter without verify_signature.

jwt.decode(.... algorithms=['HS256'])


This is from the official Changelog.

Dropped deprecated verify param in jwt.decode(...) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Use jwt.decode(encoded, key, options={"verify_signature": False}) instead.

Require explicit algorithms in jwt.decode(...) by default ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Example: jwt.decode(encoded, key, algorithms=["HS256"]).

Upvotes: 0

i-wizard
i-wizard

Reputation: 314

Yea there is definitely an issue with pyjwt > 2.0, like earlier mentioned you need to uninstall the previous version and install a more stable one like 1.7.1. it worked for me

Upvotes: 1

crispengari
crispengari

Reputation: 9333

I faced the same error when i was working with speech-to-text using ibm_watson then i solved my issue by installing PyJWT of version 1.7.1 to do that try:

pip install PyJWT==1.7.1

OR

python -m pip install PyJWT

Good Luck

Upvotes: 0

Insaurator
Insaurator

Reputation: 141

The issue may be with the newest version of PyJWT package (2.0.0). use

pip install PyJWT==1.7.1

to downgrade to the previous version and your project may work now. ( this worked for me )

Upvotes: 9

Related Questions