-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_generate.py
37 lines (30 loc) · 904 Bytes
/
token_generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import json
import requests
import base64
from dotenv import load_dotenv
import os
def get_oauth_token():
"""Get the oath token for the Idealista API
Args:
None
Return:
Token autehtication
"""
load_dotenv()
API_KEY = os.getenv("IDEALISTA_API_KEY")
API_SECRET = os.getenv("IDEALISTA_API_SECRET")
message = f'{API_KEY}:{API_SECRET}'
auth = "Basic " + base64.b64encode(message.encode("ascii")).decode("ascii")
headers_dict = {
"Authorization": auth,
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
}
data = {
"grant_type": "client_credentials",
"scope": "read"
}
response = requests.post("https://api.idealista.com/oauth/token",
headers=headers_dict,
data=data)
token = json.loads(response.text)["access_token"]
return token