Junior — Middle
How can you request an access token from the server?
sobes.tech AI
Answer from AI
To request an access token from a server in Python, the requests library is usually used. The process looks like this:
- Send an HTTP request (usually POST) to the authorization endpoint with the necessary data (for example, login and password or client_id and client_secret).
- Receive a response with the access token.
Example of an OAuth2 token request:
import requests
url = 'https://example.com/oauth/token'
data = {
'grant_type': 'password',
'username': 'user@example.com',
'password': 'password123',
'client_id': 'your_client_id',
'client_secret': 'your_client_secret'
}
response = requests.post(url, data=data)
if response.status_code == 200:
token_info = response.json()
access_token = token_info.get('access_token')
print('Access token:', access_token)
else:
print('Failed to get token:', response.status_code, response.text)
This way, you can obtain a token for subsequent requests to protected resources.