cpfair / tapiriik

tapiriik keeps your fitness in sync
https://tapiriik.com
Apache License 2.0
1.71k stars 294 forks source link

Strava OAuth problem #201

Open tompi72 opened 8 years ago

tompi72 commented 8 years ago

I try to get a local tapiriik-server running and got Garmin and Dropbox set up successfully. But trying to add Strava always results in the following feedback after the OAuth-Dialog:

Environment:

Request Method: GET
Request URL: http://XXXXX.sytes.net/auth/return/strava?state=&code=dafe49e60f3660e8f7cc4546d5864255ef91725f

Django Version: 1.8.2
Python Version: 3.4.3
Installed Applications:
('django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'tapiriik.web',
 'pipeline')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'tapiriik.web.startup.Startup',
 'tapiriik.web.startup.ServiceWebStartup',
 'tapiriik.auth.SessionAuth')

Traceback:
File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/tom/tapiriik/tapiriik/web/views/oauth/__init__.py" in authreturn
  20.         uid, authData = svc.RetrieveAuthorizationToken(req, level)
File "/home/tom/tapiriik/tapiriik/services/Strava/strava.py" in RetrieveAuthorizationToken
  96.             raise APIException("Invalid code")

Exception Type: APIException at /auth/return/strava
Exception Value: Invalid code (user None )

API-keys are all inserted, but the the answer from the Strava-server seems to be not what tapiriik is expecting...

Help would be very appreciated. I'm a runner - not an IT-professional ;)

pliguori commented 8 years ago

if you look at the code, your error is happening right here, so either STRAVA_CLIENT_ID or STRAVA_CLIENT_SECRET are not read correctly from the app.

def RetrieveAuthorizationToken(self, req, level):
    code = req.GET.get("code")
    params = {"grant_type": "authorization_code", "code": code, "client_id": STRAVA_CLIENT_ID, "client_secret": STRAVA_CLIENT_SECRET, "redirect_uri": WEB_ROOT + reverse("oauth_return", kwargs={"service": "strava"})}

    response = requests.post("https://www.strava.com/oauth/token", data=params)
    if response.status_code != 200:
        raise APIException("Invalid code")
    data = response.json()

    authorizationData = {"OAuthToken": data["access_token"]}
    # Retrieve the user ID, meh.
    id_resp = requests.get("https://www.strava.com/api/v3/athlete", headers=self._apiHeaders(ServiceRecord({"Authorization": authorizationData})))
    return (id_resp.json()["id"], authorizationData)

for me it works fine, and the local_settings.py contains the following:

STRAVA_CLIENT_SECRET = "608d25############################3c7d62a" STRAVA_CLIENT_ID = "#####" STRAVA_RATE_LIMITS = [30000]