Created
July 26, 2013 14:16
-
-
Save yanunon/6089204 to your computer and use it in GitHub Desktop.
模拟登录人人网授权OATUH2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
''' | |
Created on 2013-07-26 20:48 | |
@author: Yang Junyong <yanunon@gmail.com> | |
''' | |
import urllib2 | |
import urllib | |
import cookielib | |
import time | |
import sys | |
import os | |
from bs4 import BeautifulSoup as BS | |
APP_ID = '' | |
APP_KEY = '' | |
APP_SECRET = '' | |
REDIRECT_URL = 'http://apps.renren.com/zcj_app' | |
SCOPE = 'read_user_feed+read_user_album+read_user_blog+read_user_comment+read_user_status ' | |
LOGIN_PAGE_URL = 'https://graph.renren.com/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code&scope=%s' | |
POST_LOGIN_URL = 'https://graph.renren.com/login' | |
POST_GRANT_URL = 'http://graph.renren.com/oauth/grant' | |
GET_ACCESS_TOKEN_URL = 'https://graph.renren.com/oauth/token?grant_type=authorization_code&client_id=%s&redirect_uri=%s&client_secret=%s&code=%s' | |
POST_LOGIN_NEED_ITEM = ['redirect_uri', 'porigin', 'origin', 'appId', 'display'] | |
POST_GRANT_NEED_ITEM = ['porigin', 'redirect_uri', 'client_id', 'response_type', 'scope', 'state', 'display', 'post_form_id', 'authorizeOrigin'] | |
def get_oauth_code(user_id, user_passwd): | |
login_page_url = LOGIN_PAGE_URL % (APP_KEY, REDIRECT_URL, SCOPE) | |
resp = urllib2.urlopen(login_page_url) | |
page = resp.read() | |
bs = BS(page) | |
inputs = bs.find_all('input') | |
post_data = {} | |
for ip in inputs: | |
ip_name = ip.get('name', None) | |
if ip_name and ip_name in POST_LOGIN_NEED_ITEM: | |
post_data[ip_name] = ip['value'] | |
post_data['email'] = user_id | |
post_data['username'] = user_id | |
post_data['password'] = user_passwd | |
post_data['icode'] = '' | |
post_data['isNeedicode'] = '' | |
post_data = urllib.urlencode(post_data) | |
time.sleep(0.5) | |
resp = urllib2.urlopen(POST_LOGIN_URL, post_data) | |
url = resp.geturl() | |
if url.startswith(REDIRECT_URL): | |
code = url[url.find('code')+5:] | |
return code | |
page = resp.read() | |
bs = BS(page) | |
inputs = bs.find_all('input') | |
post_data = {} | |
for ip in inputs: | |
ip_name = ip.get('name', None) | |
if ip_name and ip_name in POST_GRANT_NEED_ITEM: | |
post_data[ip_name] = ip['value'] | |
post_data = urllib.urlencode(post_data) | |
time.sleep(0.5) | |
resp = urllib2.urlopen(POST_GRANT_URL, post_data) | |
url = resp.geturl() | |
if url.startswith(REDIRECT_URL): | |
code = url[url.find('code')+5:] | |
return code | |
else: | |
return None | |
def get_access_token(code): | |
url = GET_ACCESS_TOKEN_URL % (APP_KEY, REDIRECT_URL, APP_SECRET, code) | |
resp = urllib2.urlopen(url) | |
print resp.read() | |
if __name__ == '__main__': | |
if len(sys.argv) < 3: | |
print 'Usage: ./renren_login.py user_id user_passwd' | |
sys.exit(1) | |
user_id = sys.argv[1] | |
user_passwd = sys.argv[2] | |
cookie = cookielib.CookieJar() | |
cookie_opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) | |
urllib2.install_opener(cookie_opener) | |
code = get_oauth_code(user_id, user_passwd) | |
get_access_token(code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment