Skip to content

Instantly share code, notes, and snippets.

@zaki-yama
Created January 25, 2015 10:35
Show Gist options
  • Save zaki-yama/aabcca03070896a7743c to your computer and use it in GitHub Desktop.
Save zaki-yama/aabcca03070896a7743c to your computer and use it in GitHub Desktop.
Google App Engine + django で Google Tasks API を使う(2) 認証とリダイレクトまで
# -*- coding: utf-8 -*-
from google.appengine.ext import ndb
from oauth2client.appengine import CredentialsNDBProperty
class CredentialsModel(ndb.Model):
credentials = CredentialsNDBProperty()
# -*- coding: utf-8 -*-
from django.conf.urls import patterns
from .views import MainView, AuthView
urlpatterns = patterns('',
(r'^oauth2callback', AuthView.as_view()),
(r'^$', MainView.as_view()),
)
# -*- coding: utf-8 -*-
import logging
import os
from google.appengine.api import users
from apiclient.discovery import build
from oauth2client.appengine import StorageByKeyName
from oauth2client.client import flow_from_clientsecrets
from django.http import HttpResponseRedirect
from django.views.generic.base import View
from .models import CredentialsModel
logger = logging.getLogger(__name__)
service = build('tasks', 'v1')
SCOPE = 'https://www.googleapis.com/auth/tasks'
CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), '..', 'client_secrets.json')
FLOW = flow_from_clientsecrets(
CLIENT_SECRETS,
scope=SCOPE,
redirect_uri='http://localhost:8080/task_manager/oauth2callback')
class MainView(View):
def get(self, request):
if users.get_current_user():
authorize_url = FLOW.step1_get_authorize_url()
logger.info(authorize_url)
return HttpResponseRedirect(authorize_url)
else:
login_url = users.create_login_url(request.get_full_path())
return HttpResponseRedirect(login_url)
class AuthView(View):
def get(self, request):
credentials = FLOW.step2_exchange(request.REQUEST)
storage = StorageByKeyName(
CredentialsModel, 'admin@example.com', 'credentials')
storage.put(credentials)
return HttpResponseRedirect('/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment