Skip to content

Instantly share code, notes, and snippets.

@azyobuzin
Created March 12, 2014 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azyobuzin/9505916 to your computer and use it in GitHub Desktop.
Save azyobuzin/9505916 to your computer and use it in GitHub Desktop.
#!/virtual/azyobuzin/local/bin/python
# -*- coding: utf-8 -*-
import shelve
import urlparse
import oauth2
from wop_core import *
from wop_core import database
from wop_core.configuration import *
def success():
output.status = 303
output.headers["Location"] = base_uri
output.text = u"""<!DOCTYPE html>
<html>
<head>
<meta name="robots" content="noindex">
<title>Redirecting - Write On Photo</title>
</head>
<body>
<p><a href="%s">Back to top</a></p>
</body>
</html>""" % base_uri
def failed(status, msg):
output.status = status
output.text = u"""<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>認証エラー - Write On Photo</title>
<meta name="robots" content="noindex">
</head>
<body>
<p>%s</p>
</body>
</html>""" % msg
if "request_secret" in sess.data and "oauth_token" in env_form and "oauth_verifier" in env_form:
oauth_token = env_form["oauth_token"].value
oauth_verifier = env_form["oauth_verifier"].value
token = oauth2.Token(oauth_token, sess.data["request_secret"])
token.set_verifier(oauth_verifier)
client = oauth2.Client(twitter_consumer, token)
resp, content = client.request("https://api.twitter.com/oauth/access_token", method = "POST", headers = { "User-Agent": user_agent })
access_token = urlparse.parse_qs(content)
del sess.data["request_secret"]
with database.MySqlWrapping() as db:
user_id = access_token["user_id"][0]
sqlresult = db.fetchone("SELECT owner FROM twitter_ids WHERE id = %s", user_id)
if sqlresult is None or str(sqlresult[0]) == user_id:
#新規登録 or ログイン
udata = open_user(user_id)
#TODO:設定初期化
udata.close()
sess.data["id"] = user_id
db.execute("REPLACE INTO twitter_ids VALUES (%s, %s, %s, %s, %s)",
(user_id, access_token["screen_name"][0], user_id, access_token["oauth_token"][0], access_token["oauth_token_secret"][0]))
db.commit()
success()
else:
#関連付けられているアカウント
failed(403, "他のアカウントに関連付けられているため、ログインには使用できません。")
else:
failed(400, "oauth_token つけないなんてひどい!信じられない!!")
close()
#!/virtual/azyobuzin/local/bin/python
# -*- coding: utf-8 -*-
import urlparse
import oauth2
from wop_core import *
from wop_core.configuration import *
token = oauth2.Token("", "")
token.set_callback(create_site_uri("callback_twitter.cgi"))
client = oauth2.Client(twitter_consumer)
resp, content = client.request("https://api.twitter.com/oauth/request_token", method = "POST", headers = { "User-Agent": user_agent })
token = urlparse.parse_qs(content)
sess.data["request_secret"] = token["oauth_token_secret"][0]
sess.data["twitter_callback_type"] = CALLBACK_TYPE_LOGIN
redirect_to = "https://api.twitter.com/oauth/authorize?oauth_token=" + token["oauth_token"][0]
output.status = 303
output.headers["Location"] = redirect_to
output.text = u"""<!DOCTYPE html>
<html>
<head>
<meta name="robots" content="noindex">
<title>Redirecting - Write On Photo</title>
</head>
<body>
<p><a href="%s">Continue login</a></p>
</body>
</html>""" % redirect_to
close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment