Bird
0
0

Given this simplified Python Flask code snippet handling token refresh, what will be the output if the refresh token is expired?

medium📝 Predict Output Q4 of 15
Rest API - Authentication and Authorization
Given this simplified Python Flask code snippet handling token refresh, what will be the output if the refresh token is expired?
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/refresh', methods=['POST'])
def refresh():
    token = request.json.get('refresh_token')
    if token == 'valid_token':
        return jsonify({'access_token': 'new_access_token'}), 200
    else:
        return jsonify({'error': 'Invalid or expired token'}), 401

# Simulate request with expired token
print(refresh())
A({'access_token': 'new_access_token'}, 200)
B({'access_token': 'expired_token'}, 200)
C({'error': 'Missing token'}, 400)
D({'error': 'Invalid or expired token'}, 401)
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the condition for token validity

    The code checks if token equals 'valid_token'; else returns error with 401.
  2. Step 2: Understand the test input

    The simulated request uses an expired token, which is not 'valid_token', so error path triggers.
  3. Final Answer:

    ({'error': 'Invalid or expired token'}, 401) -> Option D
  4. Quick Check:

    Expired token response = error 401 [OK]
Quick Trick: Expired tokens trigger 401 error response [OK]
Common Mistakes:
  • Assuming expired token returns new access token
  • Confusing 400 Bad Request with 401 Unauthorized
  • Ignoring the else branch in code

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes