Garmin connect initial auth
This commit is contained in:
82
rowers/garmin_stuff.py
Normal file
82
rowers/garmin_stuff.py
Normal file
@@ -0,0 +1,82 @@
|
||||
from rowers.imports import *
|
||||
import datetime
|
||||
import requests
|
||||
from requests_oauthlib import OAuth1,OAuth1Session
|
||||
from requests import Request, Session
|
||||
import rowers.mytypes as mytypes
|
||||
from rowers.mytypes import otwtypes
|
||||
from rowers.rower_rules import is_workout_user,ispromember
|
||||
from iso8601 import ParseError
|
||||
|
||||
import numpy
|
||||
import json
|
||||
from json.decoder import JSONDecodeError
|
||||
|
||||
from rowsandall_app.settings import (
|
||||
GARMIN_CLIENT_KEY, GARMIN_REDIRECT_URI, GARMIN_CLIENT_SECRET
|
||||
)
|
||||
|
||||
from rowers.tasks import handle_c2_import_stroke_data, handle_c2_sync
|
||||
import django_rq
|
||||
queue = django_rq.get_queue('default')
|
||||
queuelow = django_rq.get_queue('low')
|
||||
queuehigh = django_rq.get_queue('low')
|
||||
from rowers.utils import myqueue
|
||||
from rowers.models import C2WorldClassAgePerformance
|
||||
|
||||
from django.core.exceptions import PermissionDenied
|
||||
|
||||
oauth_data = {
|
||||
'client_id': GARMIN_CLIENT_KEY,
|
||||
'client_secret': GARMIN_CLIENT_SECRET,
|
||||
'redirect_uri': GARMIN_REDIRECT_URI,
|
||||
'authorization_uri': "https://connectapi.garmin.com/oauth-service/oauth/request_token",
|
||||
'content_type': 'application/x-www-form-urlencoded',
|
||||
'tokenname': 'garmintoken',
|
||||
'refreshtokenname': 'garminrefreshtoken',
|
||||
'expirydatename': 'garmintokenexpirydate',
|
||||
'bearer_auth': True,
|
||||
'base_url': "https://connect.garmin.com/oauthConfirm",
|
||||
'scope':'write',
|
||||
'headers': 'Authorization: OAuth oauth_version="1.0"'
|
||||
}
|
||||
|
||||
def garmin_authorize():
|
||||
redirect_uri = oauth_data['redirect_uri']
|
||||
client_secret = oauth_data['client_secret']
|
||||
client_id = oauth_data['client_id']
|
||||
base_uri = oauth_data['base_url']
|
||||
|
||||
|
||||
garmin = OAuth1Session(oauth_data['client_id'],
|
||||
client_secret=oauth_data['client_secret'],
|
||||
)
|
||||
fetch_response = garmin.fetch_request_token(oauth_data['authorization_uri'])
|
||||
resource_owner_key = fetch_response.get('oauth_token')
|
||||
resource_owner_secret = fetch_response.get('oauth_token_secret')
|
||||
|
||||
authorization_url = garmin.authorization_url(base_uri)
|
||||
return authorization_url
|
||||
|
||||
def garmin_processcallback(redirect_respones):
|
||||
garmin = OAuth1Session(oauth_data['client_id'],
|
||||
client_secret=oauth_data['client_secret'],
|
||||
)
|
||||
oauth_response = garmin.parse_authorization_response(redirect_response)
|
||||
|
||||
verifier = oauth_response.get('oauth_verifier')
|
||||
token = oauth_response.get('oauth_token')
|
||||
access_token_url = 'https://connectapi.garmin.com/oauth-service/oauth/access_token'
|
||||
|
||||
# Using OAuth1Session
|
||||
garmin = OAuth1Session(oauth_data['client_id'],
|
||||
client_secret=oauth_data['client_secret'],
|
||||
resource_owner_key=resource_owner_key,
|
||||
resource_owner_secret=resource_owner_secret,
|
||||
verifier=verifier,)
|
||||
oauth_tokens = garmin.fetch_access_token(access_token_url)
|
||||
|
||||
garmintoken = oauth_tokens.get('oauth_token')
|
||||
garminrefreshtoken = oauth_tokens.get('oauth_token_secret')
|
||||
|
||||
return garmintoken,garminrefreshtoken
|
||||
Reference in New Issue
Block a user