あったらしくるえるはてなくしょん

id:kskmeuk あったらしく

はてな認証API Google App Engine のサンプルソース..

Python は変わってるけど、面白いですね。セミコロンがなくてもいいのとインデントがいやでも整うのは結構すきかも。私ズボラなので...

Google App Engine から、はてな認証APIを使ってみた。 - くるえるはてなくしょん この件の .py のソースです。
テストはhttp://kskmeuk4hatena.appspot.com/

とりあえずソースを晒してみます。2018年10月31日(水) をもって、はてな認証APIの提供を終了します。それに伴い、OAuthへ移行をお願いいたします - はてなの日記 - 機能変更、お知らせなど における、http://auth.hatena.ne.jp/help/api の説明ままです。

  1. GAEでは、urlfetch をつかって読みに行かないとだめ
  2. md5はhexdigest()のほうをつかう
  3. へんなコードでごめんなさい。

読み出してからの処理については、まだ書いていないけれど、JSON を読みに行くときはDJangoのクラスを使うのがよさそうで、XMLを読みに行くときは、SimpleXMLTreeBulider を使うのがいいみたい。

とりあえず、以下がソース...

#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import wsgiref.handlers
import md5

from google.appengine.api import urlfetch
from google.appengine.api import mail
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp \
  import template
  
class HatenaAuth():
  cert = ""
  secretKey = "ここにもらった秘密鍵"
  apiKey = "ここにもらったAPIKey"
  api_sig = secretKey+"api_key"+apiKey
  certmd5 = md5.new(api_sig).hexdigest()
  certURI = "http://auth.hatena.ne.jp/auth?api_key="+ apiKey +"&api_sig="+ certmd5
  authmd5 = md5.new( api_sig + "cert" + cert).hexdigest()
  authURI = "http://auth.hatena.ne.jp/api/auth.xml?api_key="+ apiKey + "&cert="+ cert +"&api_sig=" + authmd5
  def __init__(self,cert1):
    self.cert = cert1
    self.authmd5 = md5.new( self.api_sig + "cert" + cert1).hexdigest()
    self.authURI = "http://auth.hatena.ne.jp/api/auth.json?api_key="+ self.apiKey + "&cert="+ self.cert +"&api_sig=" + self.authmd5
    
class HatenaId(db.Model):
  result = db.TextProperty( required = True )
  cert = db.StringProperty( required = True )
  date = db.DateTimeProperty(auto_now_add=True)

class MainHandler(webapp.RequestHandler):
  def get(self):
    if self.request.get("cert") != "":
      hatenaAuth = HatenaAuth(self.request.get("cert"))
      response = urlfetch.fetch(hatenaAuth.authURI)   
      hatenaId = HatenaId( result = response.content, cert=self.request.get("cert"))

      if  ": false" in str(response.content):
        data = {
                "result":hatenaId.result,
        }
        hatenaId.put()
        self.response.out.write(template.render('finish.html',data))
    else:
      values = {}
      self.response.out.write(template.render('main.html',values))
        
  def post(self):
    hatenaAuth = HatenaAuth("")
    self.redirect(hatenaAuth.certURI)
    
def main():
  application = webapp.WSGIApplication([ ('/', MainHandler)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
  main()