Closed Selwan7131 closed 10 months ago
Same issue here with:
`import requests
client_id = 'xxx' client_secret = 'xxx' redirect_url = 'xxx'
auth_url = f'https://flow.polar.com/oauth2/authorization?response_type=code&client_id={client_id}&redirect_uri={redirect_url}'
print(f'Visit this URL to authorize your application: {auth_url}')
auth_code = input('Enter the authorization code: ')
token_url = 'https://polarremote.com/v2/oauth2/token' token_data = { 'grant_type': 'authorization_code', 'code': auth_code, 'redirect_uri': redirect_url, 'client_id': client_id, 'client_secret': client_secret }
print(token_data) # Add this line to print the token_data dictionary token_response = requests.post(token_url, data=token_data) print(token_response.status_code) # Add this line to print the response status code print(token_response.headers) # Add this line to print the response headers
token_json = token_response.json() print(token_json) # Add this line to print the JSON response
access_token = token_response.json()['access_token']
user_id_url = 'https://www.polaraccesslink.com/v3/users' headers = {'Authorization': f'Bearer {access_token}'} user_id_response = requests.get(user_id_url, headers=headers) user_id = user_id_response.json()['polar_user_id']
activities_url = f'https://www.polaraccesslink.com/v3/users/{user_id}/exercise' activities_response = requests.get(activities_url, headers=headers) activities = activities_response.json()['exercises']
for activity in activities: print(f'Activity: {activity["start_time"]}') print(f'Heart rate: {activity["heart_rate"]["average"]} bpm')
sleep_url = f'https://www.polaraccesslink.com/v3/users/{user_id}/sleep' sleep_response = requests.get(sleep_url, headers=headers) sleep_data = sleep_response.json()['sleeps']
for sleep in sleep_data: print(f'Sleep: {sleep["date"]}') print(f'Sleep stages: {sleep["sleep_stages"]}') `
user_id_url = 'https://www.polaraccesslink.com/v3/users' headers = {'Authorization': f'Bearer {access_token}'} user_id_response = requests.get(user_id_url, headers=headers) I do exactly the same as here but I get a 405 reponse
Hi! Please make sure that you follow correct authentication flow. There has been some improvements to the documentation and it should describe it more clearly now. Pay attention especially to the fact that user needs to be registered (7) before the data is accessible. More information here: https://www.polar.com/accesslink-api/#polar-accesslink-api
Here are the steps
Also make sure that you are using correct HTTP method, error 405 indicates that incorrect method is used.
I want to access the data of a polar device. I receive the authorization code but when trying to get an access token I get 'error': 'unauthorized_client'.
`authorize_url = 'https://flow.polar.com/oauth2/authorization' redirect_uri = 'http://localhost:8000/callback' scope = 'accesslink.read_all'
params = { 'response_type': 'code', 'client_id': client_id, 'redirect_uri': redirect_uri, 'scope': scope }
url = f"{authorize_url}?{'&'.join([f'{k}={v}' for k,v in params.items()])}"
webbrowser.open(url)
class CallbackHandler(http.server.SimpleHTTPRequestHandler): code = None
with socketserver.TCPServer(("", 8000), CallbackHandler) as httpd: httpd.handle_request()
headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': f'Basic {client_id}:{client_secret}', 'Accept': 'application/json', }
data = { 'grant_type': 'authorization_code', 'code': CallbackHandler.code, }
r = requests.post('https://polarremote.com/v2/oauth2/token', data=data, headers=headers)
print(r.json()) `