Recent site activity

Python SP Code Sample

Scroll to the bottom for sample files you can download.  The sections below also contain a line-numbered copy for quick reference.


oauth/demo/app.yaml
1: application: oauth-demo
2: version: 1
3: runtime: python
4: api_version: 1
5: 
6: handlers:
7: - url: .*
8:   script: oauth-demo.py
oauth/demo/oauth-demo.py
 1: #!/usr/bin/env python
 2: #
 3: # Copyright 2009 Google Inc.
 4: #
 5: # Licensed under the Apache License, Version 2.0 (the "License");
 6: # you may not use this file except in compliance with the License.
 7: # You may obtain a copy of the License at
 8: #
 9: #     http://www.apache.org/licenses/LICENSE-2.0
10: #
11: # Unless required by applicable law or agreed to in writing, software
12: # distributed under the License is distributed on an "AS IS" BASIS,
13: # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: # See the License for the specific language governing permissions and
15: # limitations under the License.
16: #
17: 
18: """Simple app to demonstrate OAuth support.
19: 
20: Exposes one handler that responds to 2-legged OAuth requests.
21: """
22: 
23: 
24: import logging
25: import sys
26: from google.appengine.ext import webapp
27: import oauth
28: import wsgiref.handlers
29: 
30: 
31: class OAuth2LeggedHandler(webapp.RequestHandler):
32:   """Handler for 2-legged OAuth requests."""
33: 
34:   def get(self):
35:     """Handle GET events."""
36:     try:
37:       out = ('Signature is valid. OAuth consumer key: %s'
38:              % oauth.get_oauth_consumer_key())
39:     except oauth.InvalidOAuthRequestError, e:
40:       out = 'InvalidOAuthRequestError: %s' % e
41:     except oauth.OAuthError, e:
42:       out = 'OAuthError: %s' % e
43:     except Exception, e:
44:       out = 'Exception: %s' % e
45:     finally:
46:       self.response.out.write(out)
47:       logging.info(out)
48: 
49:   def post(self):
50:     """Handle POST events."""
51:     self.get()
52: 
53: 
54: # Map URLs to our RequestHandler classes above
55: _URLS = [
56:     ('/oauth2legged', OAuth2LeggedHandler),
57: ]
58: 
59: 
60: def main(argv):
61:   application = webapp.WSGIApplication(_URLS, debug=True)
62:   wsgiref.handlers.CGIHandler().run(application)
63: 
64: if __name__ == '__main__':
65:   main(sys.argv)
oauth/demo/oauth.py
1: #!/usr/bin/env python
 2: #
 3: # Copyright 2009 Google Inc.
 4: #
 5: # Licensed under the Apache License, Version 2.0 (the "License");
 6: # you may not use this file except in compliance with the License.
 7: # You may obtain a copy of the License at
 8: #
 9: #     http://www.apache.org/licenses/LICENSE-2.0
10: #
11: # Unless required by applicable law or agreed to in writing, software
12: # distributed under the License is distributed on an "AS IS" BASIS,
13: # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: # See the License for the specific language governing permissions and
15: # limitations under the License.
16: #
17: 
18: """Python library for OAuth support.
19: 
20: Classes defined here:
21:   OAuthError
22:   InvalidOAuthRequestError
23: """
24: 
25: 
26: from google.appengine.api import apiproxy_stub_map
27: from google.appengine.api import user_service_pb
28: from google.appengine.api import users
29: from google.appengine.runtime import apiproxy_errors
30: 
31: 
32: class OAuthError(users.Error):
33:   """Base OAuth error type."""
34: 
35: 
36: class InvalidOAuthRequestError(OAuthError):
37:   """Raised if the request was invalid."""
38: 
39: 
40: def get_oauth_consumer_key():
41:   """Returns the oauth_consumer_key from the request.
42: 
43:   Returns:
44:     string
45: 
46:   Raises:
47:     InvalidOAuthRequestError: The OAuth request was malformed, contained a
48:         bad signature, or was otherwise invalid.
49:     OAuthError: An unknown error occurred.
50:   """
51:   req = user_service_pb.CheckOAuthSignatureRequest()
52:   resp = user_service_pb.CheckOAuthSignatureResponse()
53:   try:
54:     apiproxy_stub_map.MakeSyncCall('user', 'CheckOAuthSignature', req, resp)
55:   except apiproxy_errors.ApplicationError, e:
56:     if (e.application_error ==
57:         user_service_pb.UserServiceError.OAUTH_INVALID_REQUEST):
58:       raise InvalidOAuthRequestError
59:     elif (e.application_error ==
60:           user_service_pb.UserServiceError.OAUTH_ERROR):
61:       raise OAuthError
62:     else:
63:       raise e
64:   return resp.oauth_consumer_key()

Attachments (3)

  • app.yaml - on Nov 2, 2009 9:44 AM by Eric Sachs (version 1)
    1k Download
  • oauth-demo.py - on Nov 2, 2009 9:44 AM by Eric Sachs (version 1)
    2k Download
  • oauth.py - on Nov 2, 2009 9:44 AM by Eric Sachs (version 1)
    2k Download