from requests_oauthlib import OAuth1Session, OAuth1 import hmac import hashlib import requests from urllib.parse import quote_plus as rawurlencode import logging logging.basicConfig() logging.getLogger().setLevel(logging.DEBUG) requests_log = logging.getLogger("requests.packages.urllib3") requests_log.setLevel(logging.DEBUG) requests_log.propagate = True payload = { 'workoutName': '30min at threshold', 'sport': 'GENERIC', 'description': 'Uploaded from Rowsandall.com', 'estimatedDurationInSecs': 1800, 'estimatedDistanceInMeters': 6936, 'workoutProvider': 'Rowsandall.com', 'workoutSourceId': 'Rowsandall.com', 'steps': [ { 'type': 'Step', 'stepOrder': 0, 'repeatType': 'Step', 'repeatValue': 1, 'intensity': 'Active', 'description': '0', 'durationType': 'TIME', 'durationValue': 1800, 'durationValueType': '', 'targetType': 'Power', 'targetValue': 1226, 'targetValueLow': 0, 'targetValueHigh': (0,) } ] } payload = {} oauth_consumer_key = b'ca29ba5e-6868-4468-987d-4ee60a1f04bf' oauth_consumer_secret = b'SKRqjML9mOBV7BcPpN7LsbuDNDtvLOvRiyo' oauth_token = b'79454eab-bf82-4329-9de2-82a6bd911498' oauth_token_secret = b'DihdHJ2ThEdbsyoStpPTEmYh5F52L697HhD' authheaders = OAuth1(client_key=oauth_consumer_key, client_secret=oauth_consumer_secret, resource_owner_key=oauth_token, resource_owner_secret=oauth_token_secret, signature_method='HMAC-SHA1', #encoding='base64' ) url = 'https://apis.garmin.com/training-api/workout/' response = requests.post(url,payload,auth=authheaders) # build base_string base_string1 = b'POST&https%3A%2F%2Fapis.garmin.com%2Ftraining-api%2Fworkout%2F&description%3DUploaded%2520from%2520Rowsandall.com%26estimatedDistanceInMeters%3D6936%26estimatedDurationInSecs%3D1800%26oauth_consumer_key%3Dca29ba5e-6868-4468-987d-4ee60a1f04bf%26oauth_nonce%3D163208869057942765101620205416%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1620205416%26oauth_token%3D79454eab-bf82-4329-9de2-82a6bd911498%26oauth_version%3D1.0%26sport%3DGENERIC%26steps%3Ddescription%26steps%3DdurationType%26steps%3DdurationValue%26steps%3DdurationValueType%26steps%3Dintensity%26steps%3DrepeatType%26steps%3DrepeatValue%26steps%3DstepOrder%26steps%3DtargetType%26steps%3DtargetValue%26steps%3DtargetValueHigh%26steps%3DtargetValueLow%26steps%3Dtype%26workoutName%3D30min%2520at%2520threshold%26workoutProvider%3DRowsandall.com%26workoutSourceId%3DRowsandall.com' base_stringa = b'POST&https%3A%2F%2Fapis.garmin.com%2Ftraining-api%2Fworkout%2F&oauth_consumer_key%3Dca29ba5e-6868-4468-987d-4ee60a1f04bf%26oauth_nonce%3D90559685229655402871620208938%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1620208938%26oauth_token%3D79454eab-bf82-4329-9de2-82a6bd911498%26oauth_version%3D1.0' base_string2 = b'POST&https%3A%2F%2Fapis.garmin.com%2Ftraining-api%2Fworkout%2F&oauth_consumer_key%3Dca29ba5e-6868-4468-987d-4ee60a1f04bf%26oauth_nonce%3DIyk9Ambokd2%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1620138340%26oauth_token%3D673806b7-aa7b-4064-8290-2dd1b0236ae6%26oauth_version%3D1.0' base_string3 = b'POST&https%3A%2F%2Fapis.garmin.com%2Ftraining-api%2Fworkout&oauth_consumer_key%3Dca29ba5e-6868-4468-987d-4ee60a1f04bf%26oauth_nonce%3D90559685229655402871620208938%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1620208938%26oauth_token%3D79454eab-bf82-4329-9de2-82a6bd911498%26oauth_version%3D1.0' auth = b'&'.join([oauth_consumer_secret,oauth_token_secret]) signingKey = bytes(rawurlencode(oauth_consumer_secret) + "&" + rawurlencode(oauth_token_secret),'utf-8') print(auth) print(signingKey) print(base_string3) sig = hmac.new(signingKey, base_string3, hashlib.sha1) print(response.status_code) print(response.text) print(sig.hexdigest().encode())