Search code examples
pythonuber-api

Retrieve data with Uber Rides Python SDK fails with 500 error


Having followed this tutorial https://developer.uber.com/docs/riders/ride-requests/tutorials/api/python , I'm getting errors when trying to fetch data such as user profile or activity.

Here is my sample(Flask) code:

import os
from datetime import datetime
from flask import Flask, request, flash, url_for, redirect, \
     render_template, abort, send_from_directory, redirect

from uber_rides.auth import AuthorizationCodeGrant
from uber_rides.session import Session
from uber_rides.client import UberRidesClient

app = Flask(__name__)
app.config.from_pyfile('flaskapp.cfg')

# Configure Uber Oauth2.0
auth_flow = AuthorizationCodeGrant(
    '<client_id>',
    ['profile', 'history'],
    '<client_secret>',
    'http://localhost:8080/callback'
    )

@app.route('/')
def index():
    auth_url = auth_flow.get_authorization_url()
    return redirect(auth_url, code=302)

@app.route('/callback')
def callback():
    session = auth_flow.get_session(request.url)
    client = UberRidesClient(session)
    credentials = session.oauth2credential

    # Fetch users profile
    response = client.get_user_profile()
    profile = response.json

    first_name = profile.get('first_name')
    last_name = profile.get('last_name')
    email = profile.get('email')

    # Fetch user activity
    response = client.get_user_activity()
    history = response.json

    return first_name, 200

if __name__ == '__main__':
    app.run(debug=True)

And trace of errors:

Starting WSGIServer type flask on 127.0.0.1:8080 ...
 * Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
127.0.0.1 - - [11/Jan/2017 21:11:53] "GET / HTTP/1.1" 302 -
127.0.0.1 - - [11/Jan/2017 21:12:09] "GET /callback?state=VFI3w30Haz5MUb1jvaLxDsCdMduVedLv&code=NSol8RDwh5zbLYL4ef0U96ePjsZZ6l HTTP/1.1" 500 -
Error on request:
Traceback (most recent call last):
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/werkzeug/serving.py", line 205, in run_wsgi
    execute(self.server.app)
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/werkzeug/serving.py", line 193, in execute
    application_iter = app(environ, start_response)
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/flask/app.py", line 1994, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/flask/app.py", line 1994, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/crazywizard/Documents/uber/flaskapp.py", line 41, in callback
    response = client.get_user_activity()
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/uber_rides/client.py", line 260, in get_user_activity
    return self._api_call('GET', 'v1.2/history', args=args)
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/uber_rides/client.py", line 104, in _api_call
    return request.execute()
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/uber_rides/request.py", line 152, in execute
    return self._send(prepared_request)
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/uber_rides/request.py", line 136, in _send
    response = session.send(prepared_request)
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/requests/sessions.py", line 615, in send
    r = dispatch_hook('response', hooks, r, **kwargs)
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/requests/hooks.py", line 31, in dispatch_hook
    _hook_data = hook(hook_data, **kwargs)
  File "/Users/crazywizard/Documents/uber/venv/lib/python2.7/site-packages/uber_rides/utils/handlers.py", line 63, in error_handler
    raise ServerError(response, error_message)
ServerError: 500: We have experienced a problem.

Could this be a problem with Uber's service?

Update: Debug trace on requests.

Starting WSGIServer type flask on 127.0.0.1:8080 ...
INFO:werkzeug: * Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
INFO:werkzeug:127.0.0.1 - - [15/Jan/2017 18:58:09] "GET / HTTP/1.1" 302 -
DEBUG:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): login.uber.com
DEBUG:requests.packages.urllib3.connectionpool:https://login.uber.com:443 "POST /oauth/token HTTP/1.1" 200 None
INFO:werkzeug:127.0.0.1 - - [15/Jan/2017 18:58:19] "GET /callback?state=z9ga01Y6kgNFRbgDbeEA3IjCYLMTnI7M&code=wwTzfIbFivsCoGE4RIu0rDb2fO22NI HTTP/1.1" 200 -
DEBUG:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): sandbox-api.uber.com
DEBUG:requests.packages.urllib3.connectionpool:https://sandbox-api.uber.com:443 "GET /v1.2/me HTTP/1.1" 500 75
INFO:werkzeug:127.0.0.1 - - [15/Jan/2017 18:58:35] "GET /profile HTTP/1.1" 500 -
ERROR:werkzeug:Error on request:

UPDATE 2: Requests module logging

Starting WSGIServer type flask on 127.0.0.1:8080 ...
INFO:werkzeug: * Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
^C(venv) Davids-MacBook-Pro:uber crazywizard$ clear
(venv) Davids-MacBook-Pro:uber crazywizard$ python app.py
Starting WSGIServer type flask on 127.0.0.1:8080 ...
INFO:werkzeug: * Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
INFO:werkzeug:127.0.0.1 - - [17/Jan/2017 22:35:04] "GET / HTTP/1.1" 302 -
DEBUG:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): login.uber.com
send: 'POST /oauth/token HTTP/1.1\r\nHost: login.uber.com\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.12.4\r\nContent-Length: 217\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\ncode=Mg1VBWKR0V0ioeLYTAv61pAbjy3WD4&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fcallback&client_id=xxx&client_secret=xxx&grant_type=authorization_code'
reply: 'HTTP/1.1 200 OK\r\n'
header: Server: nginx
header: Date: Tue, 17 Jan 2017 19:35:08 GMT
header: Content-Type: application/json
header: Transfer-Encoding: chunked
header: Connection: keep-alive
header: Pragma: no-cache
header: Cache-Control: no-store
header: Set-Cookie: session=ab5c70960ceb1415_587e71ec.pJiJyH80bf4wULzs_NpDCMc-Ymw; Domain=login.uber.com; Secure; HttpOnly; Path=/
header: X-Uber-App: login
header: Strict-Transport-Security: max-age=604800
header: X-Content-Type-Options: nosniff
header: X-XSS-Protection: 1; mode=block
header: Strict-Transport-Security: max-age=2592000
header: X-Frame-Options: SAMEORIGIN
header: Cache-Control: max-age=0
header: Content-Encoding: gzip
DEBUG:requests.packages.urllib3.connectionpool:https://login.uber.com:443 "POST /oauth/token HTTP/1.1" 200 None
DEBUG:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): sandbox-api.uber.com
send: 'GET /v1/products?latitude=37.77&longitude=-122.41 HTTP/1.1\r\nHost: sandbox-api.uber.com\r\nAccept-Encoding: identity\r\nX-Uber-User-Agent: Python Rides SDK v0.2.7\r\nAuthorization: Bearer xxx\r\n\r\n'
reply: 'HTTP/1.1 500 Internal Server Error\r\n'
header: Server: nginx
header: Date: Tue, 17 Jan 2017 19:35:09 GMT
header: Content-Type: application/json
header: Content-Length: 75
header: Connection: keep-alive
header: X-Uber-App: uberex-sandbox
header: X-Uber-App: migrator-uberex-sandbox-optimus
header: Strict-Transport-Security: max-age=604800
header: X-Content-Type-Options: nosniff
header: X-XSS-Protection: 1; mode=block
DEBUG:requests.packages.urllib3.connectionpool:https://sandbox-api.uber.com:443 "GET /v1/products?latitude=37.77&longitude=-122.41 HTTP/1.1" 500 75
INFO:werkzeug:127.0.0.1 - - [17/Jan/2017 22:35:10] "GET /callback?state=dH1nnut35q9xcOdC3W9a1lFKNqGXn9h5&code=Mg1VBWKR0V0ioeLYTAv61pAbjy3WD4 HTTP/1.1" 500 -
ERROR:werkzeug:Error on request:

Update 3: I have dug into the rides_python_sdk and added extra headers as shown in the docs https://developer.uber.com/docs/riders/ride-requests/tutorials/api/curl#get-a-list-of-products-available

I also corrected the Accept-Encoding param to what is in the sdk ['gzip, deflate']

Still getting the 500 error

DEBUG:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): api.uber.com
send: 'GET /v1.2/products?latitude=37.77&longitude=-122.41 HTTP/1.1\r\nHost: api.uber.com\r\nAccept-Language: en_EN\r\nX-Uber-User-Agent: Python Rides SDK v0.3.1\r\nContent-Type: application/json\r\nAccept-Encoding: gzip, deflate\r\nAuthorization: Bearer ppdHHakR8EQL09kpAIrPYkDCAWFHQT\r\n\r\n'
reply: 'HTTP/1.1 500 Internal Server Error\r\n'
header: Server: nginx
header: Date: Wed, 18 Jan 2017 09:46:04 GMT
header: Content-Type: application/json
header: Content-Length: 75
header: Connection: keep-alive
header: X-Uber-App: uberex-nonsandbox
header: X-Uber-App: migrator-uberex-optimus
header: Strict-Transport-Security: max-age=604800
header: X-Content-Type-Options: nosniff
header: X-XSS-Protection: 1; mode=block

Solution

  • Thanks for your patience while I investigated this issue. This was an underlying bug in our v1 oauth endpoints which impacted newly created applications. I have released a new version of the sdk that resolves this problem. Please use v0.4.0 and you should not have any issues. Thanks for the detailed feedback!