Morgan Allen Morgan Allen - 1 month ago 9
Python Question

Using Flask to access Google's API

I'm trying to create a Flask app that talks to Google's API. However, I keep getting this error:

Traceback (most recent call last):
File "hello.py", line 16, in <module>
@app.route('google_api')
File "/Library/Python/2.7/site-packages/flask/app.py", line 1013, in decorator
self.add_url_rule(rule, endpoint, f, **options)
File "/Library/Python/2.7/site-packages/flask/app.py", line 62, in wrapper_func
return f(self, *args, **kwargs)
File "/Library/Python/2.7/site-packages/flask/app.py", line 976, in add_url_rule
rule = self.url_rule_class(rule, methods=methods, **options)
File "/Library/Python/2.7/site-packages/werkzeug/routing.py", line 599, in __init__
raise ValueError('urls must start with a leading slash')
ValueError: urls must start with a leading slash


I went through the Flask tutorial and the "Using OAuth 2.0 for Web Services" on Google's website. I'm not quite sure what I'm doing wrong, and I'm not quite sure what the error is telling me. It looks like it has something to do with with Flask? Here is my code:

from flask import Flask
from oauth2client import client

app = Flask(__name__)

@app.route("/")
def hello():
return "Hello world!"

@app.route('google_api')
def google_api():
flow = client.flow_from_clientsecrets(
'client_secrets.json',
scope='https://www.googleapis.com/auth/prediction',
redirect_uri='http://127.0.0.1:5000/redirect')

@app.route('/redirect')
def redir():
return "Authenticated"


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


What am I doing wrong? How can I fix it?

Answer

I think you are missing the leading slash from your google api route.

@app.route('google_api')

Have you tried updating to

@app.route('/google_api')

Hope that helps

Comments