Question

edit - as I couldn't tag this with Strava here are the docs if you're interested - http://strava.github.io/api/

I get through the authentication fine and gain the access_token (and my athlete info) in a response.read.

I'm having problems with the next step: I want to return information about a specific activity.

    import urllib2
    import urllib

    access_token = str(tp[3]) #this comes from the response not shown
    print access_token

    ath_url = 'https://www.strava.com/api/v3/activities/108838256'

    ath_val = values={'access_token':access_token}

    ath_data = urllib.urlencode (ath_val)

    ath_req = urllib2.Request(ath_url, ath_data)

    ath_response = urllib2.urlopen(ath_req)

    the_page = ath_response.read()

    print the_page

error is

    Traceback (most recent call last):
      File "C:\Users\JordanR\Python2.6\documents\strava\auth.py", line 30, in <module>
        ath_response = urllib2.urlopen(ath_req)
      File "C:\Users\JordanR\Python2.6\lib\urllib2.py", line 124, in urlopen
        return _opener.open(url, data, timeout)
      File "C:\Users\JordanR\Python2.6\lib\urllib2.py", line 389, in open
        response = meth(req, response)
      File "C:\Users\JordanR\Python2.6\lib\urllib2.py", line 502, in http_response
        'http', request, response, code, msg, hdrs)
      File "C:\Users\JordanR\Python2.6\lib\urllib2.py", line 427, in error
        return self._call_chain(*args)
      File "C:\Users\JordanR\Python2.6\lib\urllib2.py", line 361, in _call_chain
        result = func(*args)
      File "C:\Users\JordanR\Python2.6\lib\urllib2.py", line 510, in http_error_default
        raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    HTTPError: HTTP Error 404: Not Found

the 404 is a puzzle as I know this activity exists?

Is 'access_token' the correct header information? the documentation (http://strava.github.io/api/v3/activities/#get-details) uses Authorization: Bearer ? I'm not sure how liburl would encode the Bearer part of the info?

sorry if some of my terminology is a bit off, I'm new to this.

answered this bad boy.

import requests as r
access_token = tp[3]

ath_url = 'https://www.strava.com/api/v3/activities/108838256'
header = {'Authorization': 'Bearer 4b1d12006c51b685fd1a260490_example_jklfds'}

data = r.get(ath_url, headers=header).json()

it needed the "Bearer" part adding in the Dict.

thanks for the help idClark

Was it helpful?

Solution

I prefer using the third-party Requests module. You do indeed need to follow the documentation and use the Authorization: Header documented in the API

Requests has an arg for header data. We can then create a dict where the key is Authorization and its value is a single string Bearer access_token

#install requests from pip if you want
import requests as r
url = 'https://www.strava.com/api/v3/activities/108838256'
header = {'Authorization': 'Bearer access_token'}
r.get(url, headers=header).json()

If you really want to use urllib2

#using urllib2
import urllib2
req = urllib.Request(url)
req.add_header('Authorization', 'Bearer access_token')
resp = urllib2.urlopen(req)
content = resp.read()

Just remember access_token needs to be the literal string value, eg acc09cds09c097d9c097v9

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top