File size: 1,124 Bytes
c2b923e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
import requests
import urllib.parse
import os

# Assuming environment variables are used to store sensitive data
client_id = 'Siemens.Advanta'
client_secret = os.environ.get('SIEMENS_API_KEY')
username = "I0340828"
password = os.environ["SIEMENS_EW_PW"]

# Endpoint
url = "https://eu-ecodomus-services.siemens.com/api/token HTTP/1.1"

# Data needs to be URL-encoded
data = {
    'client_id': client_id,
    'client_secret': client_secret,
    'username': username,
    'password': password,
    'grant_type': 'password'
}
encoded_data = urllib.parse.urlencode(data)
print(encoded_data)
# Headers
headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
}

# POST Request
response = requests.post(url, data=encoded_data, headers=headers)

print("Status Code:", response.status_code)
print("Response Content:", response.text)
print("content", response.content)

if response.status_code == 200:
    access_token = response.json().get('access_token')
    print("Access Token:", access_token)
else:
    print("Failed to fetch access token. Check response content for details.")