API Authentication Python not working


#1

Hey Guys,

I’am Having trouble authenticating for the trip planner API I’am using the stop departures part and I’am unsure where to put the API Key in this code. Any help would be greatly appreciated.
Thanks.

from google.transit import gtfs_realtime_pb2
import urllib

feed = gtfs_realtime_pb2.FeedMessage()
response = urllib.urlopen('URL OF YOUR GTFS-REALTIME SOURCE GOES HERE')
feed.ParseFromString(response.read())
for entity in feed.entity:
  if entity.HasField('trip_update'):
    print entity.trip_update

#2

You need to put the authentication in the header of your request. Check out the example curl commands generated in the api explorer.

I’m not a python programmer but for your example this works:

from google.transit import gtfs_realtime_pb2
import urllib2

feed = gtfs_realtime_pb2.FeedMessage()
req = urllib2.Request('https://api.transport.nsw.gov.au/v1/gtfs/realtime/sydneytrains')
req.add_header('Authorization', 'apikey xxxxxxxxxxxxx')
response = urllib2.urlopen(req)

feed.ParseFromString(response.read())
for entity in feed.entity:
  if entity.HasField('trip_update'):
    print entity.trip_update

Replace xxxxxxxxxxxxx with your api key.


#3

Thank you very much.


#4

Hi again. After running the program I get an SSL certificate error. Any ideas?

Thanks.


#5

Hi @Bobby_Dazzler, we’re looking into it. If you have any more info please let us know.

Did you get it working at all? Or has it always given you an SSL error?

Thanks,
Alex


#6

Thanks, Alex.

Yes I have always had an SSL Error.

Thanks, Bobby.