以下の内容はhttps://conta.hatenablog.com/entry/2012/01/24/220632より取得しました。


google spreadsheetのtext_dbとauthSubの連携 in Flask

メモメモ、忘れそう。

やっぱりポップアップの画面でログインできるようにしたいよね、
ってことでやってみました。

gdata.spreadsheet.text_db
の認証部分のソースコードの中身を見ると、

class DatabaseClient(object):
  """Allows creation and finding of Google Spreadsheets databases.

  The DatabaseClient simplifies the process of creating and finding Google 
  Spreadsheets and will talk to both the Google Spreadsheets API and the 
  Google Documents List API. 
  """

  def __init__(self, username=None, password=None):
    """Constructor for a Database Client. 
  
    If the username and password are present, the constructor  will contact
    the Google servers to authenticate.

    Args:
      username: str (optional) Example: jo@example.com
      password: str (optional)
    """
    self.__docs_client = gdata.docs.service.DocsService()
    self.__spreadsheets_client = (
        gdata.spreadsheet.service.SpreadsheetsService())
    self.SetCredentials(username, password)

って書いてある。
なるほどね。

あと、ちょっとググってみたら
http://code.google.com/p/gdata-python-client/wiki/AuthSubWithTextDB
に色々書いてあった。

ということで、自作のDBクラスをこんな感じでカスタマイズ!(一部省略)

class SpreadDB:
"""
....
"""
    def getAuthUrl(self, nextUrl):
        self.client = gdata.spreadsheet.text_db.DatabaseClient()
        return self.client._GetDocsClient().GenerateAuthSubURL(next=nextUrl,
                                                               scope='http://spreadsheets.google.com/feeds/ http://docs.google.com/feeds/documents/')
    def setToken(self, token):
        self.client._GetDocsClient().SetAuthSubToken(token)
        self.client._GetDocsClient().UpgradeToSessionToken()
        self.client._GetSpreadsheetsClient().SetAuthSubToken(self.client._GetDocsClient().GetAuthSubToken())
"""
....
"""

そしてこれを、Flaskのルーティングのとこに書き込み

@app.route('/login', methods=['GET'])
def login():
    token = flask.request.values.get('token','')
    if token:
        db.setToken(token)
        return flask.redirect(flask.url_for('index'))
    else:
        auth_url = db.getAuthUrl("http://localhost:5000/login")
        return render_template('index.html', auth_url=auth_url)

HTMLはテキトーにこんな感じで

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="UTF-8">
        <title>title</title>

        <script type='text/javascript' src="{{ url_for('static', filename = 'lib/jquery-1.7.1.min.js') }}"></script>

    </head>
    <body>

        <h1>Login</h1>
        <a href="{{auth_url}}">Google Sign In</a>
    </body>
</html>

動いた(・∀・)


おしまい!




以上の内容はhttps://conta.hatenablog.com/entry/2012/01/24/220632より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14