淘宝TOP获取sessionkey
来源:互联网 发布:只有我知3出来了没有 编辑:程序博客网 时间:2024/04/28 09:48
<!-- -->
管理提醒: <!-- -->
本帖被 gaolei2016 执行取消精华操作(2009-12-18) <!-- -->
<!-- -->
小弟我刚刚测试了是可以的:我的测试站测试
演示站:http://www.1taotuan.com
做TOP开发的人肯定会碰到需要SessionKey才能访问数据的情况,手工去取太麻烦了,回调又需要部署到服务器上,杯具啊!为了解决这些麻烦,我特意写了一个通过程序获取SessionKey的方法,代码如下:
说明:本程序只是方便ISV调试使用,集成二次登录是无法通过审核的,用户不会在你的网站里面输入淘宝的用户名和密码的,就好比你不会在一个山寨网站输入你的银行卡号和密码一样。
经测试,上面的程序可以获取任何类型的应用(Web,客户端)的SessionKey,ISV们有福了。
上面的程序依赖于HtmlUnit这个工具的所有Jar包,需要的请到HtmlUnit的官方网站下载。
说明:本程序只是方便ISV调试使用,集成二次登录是无法通过审核的,用户不会在你的网站里面输入淘宝的用户名和密码的,就好比你不会在一个山寨网站输入你的银行卡号和密码一样。
Copy code
package com.carver.tool;
import java.net.URLEncoder;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.TextPage;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImageInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
/**
* 登录淘宝网获取授权。
*
* @author carver.gu
* @since 1.0, Nov 11, 2009
*/
public class LoginUtil {
private static final String ONLINE_CONTAINER = "http://container.open.taobao.com/container?appkey=";
private static final Pattern P_SESSION = Pattern.compile("top\\_session=(\\w+?)&");
static {
Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.SEVERE);
Logger.getLogger("org.apache.commons.httpclient.HttpMethodBase").setLevel(Level.SEVERE);
}
public static void main(String[] args) throws Exception {
String session = getOnlineSession("app_key", "username", "password");
System.out.println("session_key: " + session);
}
public static String getOnlineSession(String appkey, String uid, String pwd) throws Exception {
String url = "http://member1.taobao.com/member/mini_login.htm?login_type=3&redirect_url=";
return getSession(url, ONLINE_CONTAINER, appkey, uid, pwd);
}
private static String getSession(String loginUrl, String topUrl, String appkey, String uid,
String pwd) throws Exception {
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
String fullUrl = loginUrl + URLEncoder.encode(topUrl + appkey, "utf-8");
HtmlPage loginPage = webClient.getPage(fullUrl);
HtmlForm loginForm = loginPage.getForms().get(0);
HtmlTextInput uidInput = loginForm.getInputByName("TPL_username");
uidInput.setValueAttribute(uid);
HtmlPasswordInput pwdInput = loginForm.getInputByName("TPL_password");
pwdInput.setValueAttribute(pwd);
HtmlButton loginButton = loginForm.getButtonByName("");
HtmlPage loginRsp = null;
try {
Page rsp = loginButton.click();
if (rsp instanceof HtmlPage) {
loginRsp = (HtmlPage) rsp;
} else if (rsp instanceof TextPage) {
TextPage textRsp = (TextPage) rsp;
return extractSession(textRsp.getContent());
} else {
return null;
}
} catch (FailingHttpStatusCodeException e) {
if (e.getStatusCode() == 404) {
return extractSession(e.getMessage());
}
} catch (Exception e) {
}
HtmlCheckBoxInput agreeCheck = (HtmlCheckBoxInput) loginRsp.getElementById("agreement");
if (agreeCheck == null) {
return extractSession(loginRsp.getWebResponse().getRequestSettings().getUrl().toString());
} else {
agreeCheck.click();
}
HtmlForm agreeForm = loginRsp.getForms().get(1);
List<HtmlElement> inputElements = agreeForm.getHtmlElementsByTagName("input");
HtmlImageInput agreeButton = null;
for (HtmlElement inputElement : inputElements) {
if (inputElement instanceof HtmlImageInput) {
agreeButton = (HtmlImageInput) inputElement;
break;
}
}
Page agreeRsp = null;
try {
agreeRsp = agreeButton.click();
} catch (FailingHttpStatusCodeException e) {
if (e.getStatusCode() == 404) {
return extractSession(e.getMessage());
}
} catch (Exception e) {
}
return extractSession(agreeRsp.getWebResponse().getRequestSettings().getUrl().toString());
}
private static String extractSession(String response) {
Matcher matcher = P_SESSION.matcher(response);
if (matcher.find()) {
return matcher.group(1);
} else {
return null;
}
}
}
经测试,上面的程序可以获取任何类型的应用(Web,客户端)的SessionKey,ISV们有福了。
上面的程序依赖于HtmlUnit这个工具的所有Jar包,需要的请到HtmlUnit的官方网站下载。
这样做是方便了用户,但好想违反了TOP的审核规则,这样做估计审核会通不过:
审核规则第二条:“2、应用不允许出现二次登陆、注册入口 ”
http://wiki.open.taobao.com/index.php/%E5%AE%A1%E6%A0%B8%E8%A7%84%E5%88%99
审核规则第二条:“2、应用不允许出现二次登陆、注册入口 ”
http://wiki.open.taobao.com/index.php/%E5%AE%A1%E6%A0%B8%E8%A7%84%E5%88%99
- 淘宝TOP获取sessionkey
- 淘宝API $sessionKey 获取方法
- 淘宝API $sessionKey 获取方法
- 基于TOP协议的登录授权方式的简单实现获取sessionKey
- 淘宝API刷新SessionKey的方法
- sessionkey
- 淘宝上取SessionKey 淘宝网开放平台API(taobao.user.get)调用示例,需SessionKey
- 基于OAuth2.0的登录验证授权方式获取sessionkey
- 初探Top:App Key,App Secret ,授权码 ,SessionKey 之间的关系
- 淘宝开放平台(TOP)的返利金额获取的rails实现(hastings)
- 淘宝开放平台(TOP)的返利金额获取的rails实现(hastings)
- sessionKey/tokenKey
- 淘宝TOP开发:(一)
- 淘宝TOP开发:(二)
- 淘宝 TOP 平台Open API入门篇
- 淘宝TOP迎战电子商务新契机
- 淘宝&CSDN “TOP博客征文大赛”
- python下调用淘宝top接口例子
- 用Eclipse + CDT + MinGW做Windows编程
- android通过广播来扫描sdcard里面的音频信息
- Ojective-C学习纪录2
- 9-1 复数类中的<<和>>运算符重载
- Linux ubuntu 10.04 和 ubuntu 11.1 安装 jdk1.6.0_31
- 淘宝TOP获取sessionkey
- solr CopyField
- 小练习
- JSP内置对象使用方法
- Word排版计算机类科研论文的格式保证
- 纯虚函数
- 2012-4-14 差点忘了这里
- 9th 浙江省赛总结
- POCO库中文编程参考指南(2)基本数据类型(Poco/Types.h)