4

My GAE app uses OAUTH 2. Currently, I manually switch the redirect_uri between http://localhost:8081/oauth2callback and myapp.appspot.com/oauth2callback depending on whether do local testing with dev_appserver or if I deploy to appspot.com.

Is there some code (e.g. using os.environ) that I can use to automate this?

Thanks!

pfalke
  • 336
  • 1
  • 9

1 Answers1

6

You can get you current domain name with os.environ['SERVER_NAME'].

You also can check this by version name:

import os
if os.environ['APPLICATION_ID'].startswith('dev'):
  pass  # it's localhost
else:
  pass  # it's uploaded on gae

Will be better to use self.uri_for('callback-uri-name', _full=True) for building callback uri if you use webapp2.

You have something like this in your routes.

app = webapp2.WSGIApplication([
    webapp2.Route(r'/oauth/callback', handler='oauth.CallbackHandler', name='callback-uri-name'),
])

Then, if you'll call self.uri_for('callback-uri-name', _full=True) you'll get something like http://localhost:8080/oauth/callback or http://app-id.appspot.com/oauth/callback.

For more information: http://webapp-improved.appspot.com/guide/routing.html#building-uris

Dmytro Sadovnychyi
  • 6,171
  • 5
  • 33
  • 60
  • Thanks, that did the trick. Can you elaborate on the buiding the callback uri with `self.uri_for('callback-uri-name', _full=True)`? Or give a link for more information? I use the gdata client (for Google Contacts API) and it simply takes the uri as an input parameter. – pfalke Apr 01 '13 at 10:44
  • Thanks! One comment: I had to remove the quotes around the hander, i.e. `app = webapp2.WSGIApplication([ webapp2.Route(r'/oauth/callback', handler=oauth.CallbackHandler, name='callback-uri-name'), ])` – pfalke Apr 02 '13 at 09:16
  • and how can i check is using JAVA? – grep Dec 30 '13 at 16:10