我正在使用pyjwt库生成JWT令牌。编码时出现此错误。我将用于编码的代码放在下面。
码:
import datetime
import jwt
PROJECT_ID = 'test-iot-core-278019'
SSL_PRIVATE_KEY_FILEPATH = 'roots.pem'
SSL_ALGORITHM = 'ES256'
def create_jwt(project_id, private_key_file, algorithm):
token = {
# The time that the token was issued at
'iat': datetime.datetime.utcnow(),
# The time the token expires.
'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=60),
# The audience field should always be set to the GCP project id.
'aud': project_id
}
# Read the private key file.
with open(private_key_file, 'r') as f:
private_key = f.read()
print('Creating JWT using {} from private key file {}'.format(algorithm, private_key_file))
return jwt.encode(token, private_key, algorithm=algorithm)
print('JWT token:')
print(create_jwt(PROJECT_ID, SSL_PRIVATE_KEY_FILEPATH, SSL_ALGORITHM))
错误:
ValueError: Could not deserialize key data.