Introduction to Google App Engine

28
Introduction to Google App Engine (GAE) Asst. Prof. Dr. Kanda Runapongsa Saikaew Warakorn Chanprasomchai Charuwit Nodthaisong Khon Kaen University

description

Introduction to Google App Engine

Transcript of Introduction to Google App Engine

Page 1: Introduction to Google App Engine

Introduction to Google App Engine (GAE)

Asst. Prof. Dr. Kanda Runapongsa Saikaew

Warakorn Chanprasomchai Charuwit Nodthaisong

Khon Kaen University

Page 2: Introduction to Google App Engine

Agenda

●  What is GAE? ●  Developing Apps on GAE ●  Getting started ●  Using Google authentication on GAE app ●  Invoking Google Calendar API ●  Using Google Cloud SQL ●  Conclusion

Page 3: Introduction to Google App Engine

What is GAE?

●  Google App Engine lets you run web applications on Google’s infrastructure

●  App Engine applications are easy to build, maintain, and scale as traffic and data storage needs grow

●  With App Engine, there are no servers to maintain

●  You can serve your app from your domain name (such as http://www.example.com/) or using a free name on the appspot.com

Page 4: Introduction to Google App Engine

Benefits of GAE

●  You only pay for what you use ●  No set-up costs and no recurring fees ●  The resources your application uses, such

as storage and bandwidth, are measured by the gigabyte, and billed at competitive rates

●  You can control the maximum amounts of resources your app can consume

●  All applications can use up to 1 GB of storage and enough CPU and bandwidth to support an efficient app serving around 5 million page views a month, absolutely free

Page 5: Introduction to Google App Engine

Developing Apps on GAE

●  Using standard Java technologies ○  Support many languages with JVM-based interpreter

or compiler ■  Java, JavaScript, Ruby

●  Using Python runtime environment ○  Once Guido van Rossum, the inventor of Python,

was in the Python App Engine team ○  First language supported on GAE

●  Using PHP runtime (preview) ●  Using Go runtime environment

(experimental)

Page 6: Introduction to Google App Engine

Getting Started (1 of 5)

1. Download Google App Engine SDK https://developers.google.com/appengine/downloads?hl=th#Google_App_Engine_SDK_for_Python

2. Create a new project using Google Cloud console Go to https://cloud.google.com/console

Page 7: Introduction to Google App Engine

Getting Started (2 of 5)

3. Create GAE Project using the Application name value the same as Project ID

Page 8: Introduction to Google App Engine

Getting Started (3 of 5)

4. In the project, you will have these files automatically created

We need to write main.py

Page 9: Introduction to Google App Engine

Getting Started (4 of 5)

import webapp2 // python library class MainHandler(webapp2.RequestHandler):

// Function def get(self):

// Write “Hello world!” on the web self.response.write('Hello world!') app = webapp2.WSGIApplication([ ('/', MainHandler) ], debug=True)

Page 10: Introduction to Google App Engine

Getting Started (5 of 5)

4. Deploy on Google app engine

5. Check the app via the browser

Page 11: Introduction to Google App Engine

Google Authentication on GAE

●  To make API calls access private data, the user that has access to the private data must grant your application access

●  Therefore, your application must be authenticated, the user must grant access for your application, and the user must be authenticated in order to grant that access

●  All of this is accomplished with OAuth 2.0 and libraries written for it.

Page 12: Introduction to Google App Engine

Google API’s Client Library for Python

●  The Google APIs Client Library for Python has special support for Google App Engine applications

●  There are two decorator classes to choose from:

•  OAuth2Decorator: Use the OAuth2Decorator class to

contruct a decorator with your client ID and secret.

•  OAuth2DecoratorFromClientSecrets: Use the

OAuth2DecoratorFromClientSecrets class to contruct a

decorator using a client_secrets.json file described in

the flow_from_secrets() section of the OAuth 2.0 page

Page 13: Introduction to Google App Engine

Using Decorators ●  Need to add a specific URL handler to your application

to handle the redirection from the authorization server back to your application

def main() application = webapp.WSGIApplication(

[ ('/', MainHandler), ('/about', AboutHandler), (decorator.callback_path,

decorator.callback_handler()), ], debug=True) run_wsgi_app(application)

Page 14: Introduction to Google App Engine

Sample Authentication Code In the following code snippet, the OAuth2Decorator class

is used to create an oauth_required decorator, and the decorator is applied to a function that accesses the Google Calendar API

decorator = OAuth2Decorator( client_id='your_client_id', client_secret='your_client_secret', scope='https://www.googleapis.com/auth/calendar') service = build('calendar', 'v3') class MainHandler(webapp.RequestHandler): @decorator.oauth_required def get(self): # Get the authorized Http object created by the

decorator. http = decorator.http() ...

Page 15: Introduction to Google App Engine

How to get Client ID and Client Secret

1. Go to http://code.google.com/apis/console 2. Go to API Access

- Click button “Create Another Client ID...” 3. Fill in information about the application

Page 16: Introduction to Google App Engine

Invoking Google Calendar API 1.  Go to https://code.google.com/apis/console and click

Services and enable Calendar API 2.  Specify OAuth2 setting and service that we want to call

decorator = OAuth2Decorator( client_id='194577071906-

tgj1to860hhjbnjio1mf7ij0pljh77kl.apps.googleusercontent.com', client_secret='y3gWfp6FIaqiusr7YYViKKPM', scope='https://www.googleapis.com/auth/calendar') service = build('calendar', 'v3')

1.  Suppose that we want to create Google calendar event ●  Need to look for request body at

https://developers.google.com/google-apps/calendar/v3/reference/events/insert

Page 17: Introduction to Google App Engine

Creating Google Calendar Event class MainPage(webapp2.RequestHandler): @decorator.oauth_required def get(self): http = decorator.http() event = { 'summary': 'Google App Engine Training', 'location': 'Thai-Nichi Institute of Technology', 'start': { 'dateTime': '2013-07-20T09:00:00.000+07:00' }, 'end': { 'dateTime': '2013-07-20T16:00:00.000+07:00' } } insert_event = service.events().insert(calendarId='primary',

body=event).execute(http=http) self.response.write("Create event success with ID: " + insert_event['id'])

Page 18: Introduction to Google App Engine

The App Asking the User for Permission

Page 19: Introduction to Google App Engine

Result of Creating Calendar Event

Page 20: Introduction to Google App Engine

Using Google Cloud SQL ●  Google Cloud SQL is a web service that allows you to

create, configure, and use relational databases that live

in Google's cloud

●  By offering the capabilities of a familiar MySQL

database, the service enables you to easily move your

data, applications, and services in and out of the cloud

●  There is a range of configurations from small instances

costing just $0.025 per hour up to high end instances

with 16GB of RAM and 100GB of data storage.

Page 21: Introduction to Google App Engine

How to Create a Instance on Cloud SQL

1. Go to Google Cloud Console at http://cloud.google.com 2. Choose project that we want to use Cloud SQL 3. At the side menu, choose Cloud SQL 4. Then click button New Instance

Page 22: Introduction to Google App Engine

Accessing Cloud SQL (1 of 4)

1. Import library rdbms from google.appengine.api import rdbms

2. Set up instance name _INSTANCE_NAME = "virtual-airport-281:prinya-

workshop" 3. Connect to database

conn = rdbms.connect(instance=_INSTANCE_NAME, database='Comment')

cursor = conn.cursor() 4.Specify SQL command to use

cursor.execute('SELECT comment, name FROM comment')

Page 23: Introduction to Google App Engine

Accessing Cloud SQL (2 of 4)

5. Using templates to display the data obtained from SQL result

templates = { 'page_title' : 'Template Workshop', 'comment' : cursor.fetchall(), } {% for row in comment %} <tr> <td>{{ row[0] }}</td> <td>{{ row[1] }}</td> </tr> {% endfor %}

Page 24: Introduction to Google App Engine

Accessing Cloud SQL (3 of 4)

If you find that there is the problem, “The rdbms API is not available because the MySQLdb library could not be loaded”,

●  Type import MySQLdb in file dev_appserver.py 6. You can use any other SQL commands such as INSERT or DELETE with the function cursor.execute()

cursor.execute("INSERT INTO comment (comment,name) VALUES (%s,%s)", (comment, name));

conn.commit(); 7. After you finish with all tasks with cloud SQL, you should issue function close()

conn.close();

Page 25: Introduction to Google App Engine

Sample Result ●  As the user type comment, and name and click button

Insert data, the app will insert data into Cloud SQL, and then display the inserted data in the table on the page

Page 26: Introduction to Google App Engine

Conclusion

●  Google App Engine (GAE) will help developers to make it easy to build scalable applications

●  There are two favorite languages for implementing apps on GAE ○  Python and Java

●  To call Google API to access data, need to use OAuth 2.0

●  To leverage existing relational databases, should use Cloud SQL

Page 27: Introduction to Google App Engine

References

https://developers.google.com/appengine/ http://stackoverflow.com/questions/1085898/

choosing-java-vs-python-on-google-app-engine

Page 28: Introduction to Google App Engine

Thank you

• Kanda Runapongsa Saikaew •  Khon Kaen University, Thailand

•  Assistant Professor of Department of Computer Engineering

•  Associate Director for Administration, Computer Center

•  [email protected] •  Twitter @krunapon •  https://plus.google.com/u/

0/118244887738724224199