polarofficial / accesslink-example-python

This is an example application that uses the Polar Open AccessLink API. https://polar.com/developers
MIT License
144 stars 68 forks source link

'error': 'unauthorized_client' #30

Closed Selwan7131 closed 10 months ago

Selwan7131 commented 1 year ago

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

def do_GET(self):
    parsed_url = urlparse.urlparse(self.path)
    query = urlparse.parse_qs(parsed_url.query)
    CallbackHandler.code = query.get('code')
    self.send_response(200)
    self.send_header('Content-type', 'text/html')
    self.end_headers()
    self.wfile.write(
        b'<html><body><h1>Authorization code received!</h1></body></html>')

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()) `

Rubbert commented 1 year ago

Same issue here with:

`import requests

Replace these with your own client ID, secret, and redirect URL

client_id = 'xxx' client_secret = 'xxx' redirect_url = 'xxx'

Step 1: Get the authorization URL

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}')

Step 2: Get the authorization code from the user

auth_code = input('Enter the authorization code: ')

Step 3: Exchange the authorization code for an access token

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']

Step 4: Get the user's Polar user ID

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']

Step 5: Get the user's heart rate data during activities

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')

Step 6: Get the user's sleep stages during the night

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"]}') `

Selwan7131 commented 1 year ago

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

hytka81 commented 10 months ago

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

  1. Create Polar Flow account if you don't have one already
  2. Go to https://admin.polaraccesslink.com/ and log in using your Polar Flow credentials
  3. Fill your application/service information and create client
  4. After client is created, remember to write down OAuth2 client id and secret for later use
  5. Obtain authorization code for the user using authorization endpoint
  6. Use authorization code to obtain access token for the user using token endpoint
  7. Register user using register user endpoint to be able to access user's data
  8. Start developing the API

Also make sure that you are using correct HTTP method, error 405 indicates that incorrect method is used.