2011年1月19日 星期三

GOOGLE APPENGINE gae 實作 YAHOO 帳號登入

這個主題好像繁體中文好像沒人寫,
反正我找這方面的SOLUTION時,
沒有看到,
幸運的在YAHOO DEVELOPER Social SDK上找到了方法
http://developer.yahoo.com/social/sdk/#python

所以就不需要自已去撈api重新撰寫gae上的yahoo帳號登入了,

有些地方要改正,
就沒有任何問題了

先下載 python的SDK
http://developer.yahoo.com/social/sdk/#python

解壓後 yahoo-yos-social-python\examples\appengine 這個目錄就是寫給gae run的,
不過還有一些部驟才會正常,

0.將聯外打通,內部gae run外部可以正常。記得對外ip!!!

1.先去 https://developer.apps.yahoo.com/
申請一個account,
並新增一個YAP(Web Base)取得Key

然後 我記得description 怎麼寫 都不會通過,
之後按取消就過了,
這可能是我的誤解,
不知是什麼原因,
大家研究一下。

2.取得Key後,
打開yosdemo.py


CONSUMER_KEY
CONSUMER_SECRET
APPLICATION_ID(在Key的上面)
CALLBACK_URL(對外ip)
填入吧!


3.可是這樣子還是不會成功的run!
之後將 \build\lib 目錄下的目錄copy至 appengine下,
然後將下面內容生成gmemsess.py 置於 目錄下


# gmemsess.py - memcache-backed session Class for Google Appengine
# Version 1.4
# Copyright 2008 Greg Fawcett 
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see .
import random
from google.appengine.api import memcache
_sidChars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
_defaultTimeout=30*60 # 30 min
_defaultCookieName='gsid'
#----------------------------------------------------------------------
class Session(dict):
 """A secure lightweight memcache-backed session Class for Google Appengine."""
 #----------------------------------------------------------
 def __init__(self,rh,name=_defaultCookieName,timeout=_defaultTimeout):
  """Create a session object.
  Keyword arguments:
  rh -- the parent's request handler (usually self)
  name -- the cookie name (defaults to "gsid")
  timeout -- the number of seconds the session will last between
             requests (defaults to 1800 secs - 30 minutes)
  """
  self.rh=rh # request handler
  self._timeout=timeout
  self._name=name
  self._new=True
  self._invalid=False
  dict.__init__(self)
  if name in rh.request.str_cookies:
   self._sid=rh.request.str_cookies[name]
   data=memcache.get(self._sid)
   if data!=None:
    self.update(data)
    # memcache timeout is absolute, so we need to reset it on each access
    memcache.set(self._sid,data,self._timeout)
    self._new=False
    return
  # Create a new session ID
  # There are about 10^14 combinations, so guessing won't work
  self._sid=random.choice(_sidChars)+random.choice(_sidChars)+\
       random.choice(_sidChars)+random.choice(_sidChars)+\
       random.choice(_sidChars)+random.choice(_sidChars)+\
       random.choice(_sidChars)+random.choice(_sidChars)
  # Added path so session works with any path
  rh.response.headers.add_header('Set-Cookie','%s=%s; path=/;'%(name,self._sid))
 #----------------------------------------------------------
 def save(self):
  """Save session data."""
  if not self._invalid:
   memcache.set(self._sid,self.copy(),self._timeout)
 #----------------------------------------------------------
 def is_new(self):
  """Returns True if session was created during this request."""
  return self._new
 #----------------------------------------------------------
 def get_id(self):
  """Returns session id string."""
  return self._sid
 #----------------------------------------------------------
 def invalidate(self):
  """Delete session data and cookie."""
  self.rh.response.headers.add_header('Set-Cookie',
    '%s=; expires=Sat, 1-Jan-2000 00:00:00 GMT;'%(self._name))
  memcache.delete(self._sid)
  self.clear()
  self._invalid=True








4.最後,開啟網站會移至yahoo登入,登入會移回原來你的網站,看到自已的名字等資訊便大功告成了!!!

1 則留言:

  1. 我的作法是在GAE上面把Authentication設定為Federated Login. 自已在實作一個openid_login的頁面. 就可以直接在GAE上使用openid登入yahoo的帳戶。不需要特別去申請一個YAP (ref. http://goo.gl/jBww)

    回覆刪除