You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Version: clerk-backend-api 1.7.0
Possible related issue: #67
Summary: Passing in expires_in_seconds (as int or float) into clerk_client.create_session_token results in 400
Application: Testing backend auth
ERROR - Failed to create session token using SDK: API error occurred: Status 400
{"errors":[{"message":"Request body invalid","long_message":"The request body is invalid. Please consult the API documentation for more information."
,"code":"request_body_invalid"}],"clerk_trace_id":"2efb09d4de0199f552e1616b597cf0eb"}
Here is code basis to reproduce, which includes a working manual request method and the non-working sdk method:
"""Utility for creating Clerk session tokens"""importloggingfromtypingimportOptionalimporthttpxfromclerk_backend_apiimportClerklogger=logging.getLogger(__name__)
formatter=logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
handler=logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
def_create_session_token_sdk(clerk_client: Clerk, session_id: str) ->Optional[str]:
"""Create a session token using the Clerk SDK (currently not working)"""try:
token_response=clerk_client.sessions.create_session_token(
session_id=session_id,
expires_in_seconds=3600,
)
returntoken_response.jwtiftoken_responseelseNoneexceptExceptionase:
logger.error(f"Failed to create session token using SDK: {str(e)}")
returnNonedef_create_session_token_manual(clerk_client: Clerk, session_id: str) ->Optional[str]:
"""Create a session token using direct HTTP request"""try:
url=f"https://api.clerk.com/v1/sessions/{session_id}/tokens"headers= {
"Authorization": f"Bearer {clerk_client.sdk_configuration.security.bearer_auth}",
"Content-Type": "application/json",
}
payload= {"expires_in_seconds": 3600}
response=httpx.post(url, json=payload, headers=headers)
ifresponse.status_code==200:
returnresponse.json()["jwt"]
else:
logger.error(f"Failed to create session token: {response.status_code}")
logger.error(f"Response: {response.text}")
returnNoneexceptExceptionase:
logger.error(f"Failed to create session token manually: {str(e)}")
returnNonedefcreate_session_token(clerk_client: Clerk, clerk_user_id: str) ->Optional[str]:
"""Create a session token for a Clerk user"""try:
# First create a session for the userlogger.info(f"Creating session for user {clerk_user_id}")
session=clerk_client.sessions.create_session(
request={
"user_id": clerk_user_id,
}
)
ifnotsession:
logger.error(f"Failed to create session for user {clerk_user_id}")
returnNonelogger.info(f"Created session {session.id}, creating token...")
# Using manual approach for now as SDK method is not workingjwt=_create_session_token_manual(clerk_client, session.id)
# jwt = _create_session_token_sdk(clerk_client, session.id)logger.info(f"Created session token for session {session.id}")
returnjwtexceptExceptionase:
logger.error(f"Failed to create session: {str(e)}")
returnNone
The text was updated successfully, but these errors were encountered:
Version: clerk-backend-api 1.7.0
Possible related issue: #67
Summary: Passing in
expires_in_seconds
(as int or float) intoclerk_client.create_session_token
results in 400Application: Testing backend auth
Here is code basis to reproduce, which includes a working manual request method and the non-working sdk method:
The text was updated successfully, but these errors were encountered: