ebay 如何获取用户token
2024-08-27 09:00:03
1. 首先 配置环境加载依赖的ebay SDK
下载地址 https://go.developer.ebay.com/ebay-sdks
需要在本地仓库安装下面的jar
mvn install:install-file -Dfile=C:\ebay\ebaysdkjava1055\lib\ebaycalls.jar -DgroupId=com.ebay -DartifactId=ebaycalls -Dpackaging=jar -DgeneratePom=true -Dversion=1.7.0
mvn install:install-file -Dfile=C:\ebay\ebaysdkjava1055\lib\ebaysdkcore.jar -DgroupId=com.ebay -DartifactId=ebaysdkcore -Dpackaging=jar -DgeneratePom=true -Dversion=1.7.0
mvn install:install-file -Dfile=C:\ebay\ebaysdkjava1055\lib\helper.jar -DgroupId=com.ebay -DartifactId=helper -Dpackaging=jar -DgeneratePom=true -Dversion=1.7.0、
2. 获取sessionId
import com.ebay.sdk.ApiAccount;
import com.ebay.sdk.ApiContext;
import com.ebay.sdk.call.FetchTokenCall;
import com.ebay.sdk.helper.ConsoleUtil; import java.io.IOException;
public class EbayGetSessionIdController { public static void main(String[] args) {
try {
// Instantiate ApiContext and initialize with token and Trading API URL
ApiContext apiContext = getApiContext();
obtainSessionID(apiContext);
obtainUserToken(apiContext);
} //try
catch(Exception e) {
System.out.println("Fail to get sessionID.");
e.printStackTrace();
}
} private static void obtainUserToken(ApiContext apiContext) throws Exception {
FetchTokenCall fetchTokenCall = new FetchTokenCall(apiContext);
fetchTokenCall.setSessionID("#{sessionId}");
String token = fetchTokenCall.fetchToken();
System.out.println("token :"+token);
} private static void obtainSessionID(ApiContext apiContext) throws Exception {
//Create call object and execute the call
GetSessionIDCall apiCall = new GetSessionIDCall(apiContext);
apiCall.setRuName("#{ru_name}");
String sessionID = apiCall.getSessionID();
//Handle the result returned
System.out.println("sessionID : " + sessionID);
String url = "https://signin.ebay.com/ws/eBayISAPI.dll?SignIn&runame=#{ru_name}&SessID=" + sessionID;
System.out.println("url: "+url);
openBrowser(url);
} public static boolean openBrowser(String url) {
if (url == null) return false;
String[] unixBrowser = new String[] { "google-chrome", "firefox" };
boolean success = false;
if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
try {
Runtime.getRuntime().exec(
new String[] { "rundll32.exe", "url.dll,FileProtocolHandler", url });
success = true;
} catch (Exception e) {
}
} else {
for (int i = 0; i < unixBrowser.length; ++i)
try {
Runtime.getRuntime().exec(new String[] { unixBrowser[0], url });
success = true;
break;
} catch (Exception e) {
}
}
return success;
} // Initializes ApiContext with token and eBay API server URL
private static ApiContext getApiContext() throws IOException { String input;
ApiContext apiContext = new ApiContext();
//set Api Server Url
input = ConsoleUtil.readString("Enter eBay SOAP server URL (e.g., https://api.ebay.com/wsapi): ");
apiContext.setApiServerUrl(input);
ApiAccount apiAccount = new ApiAccount();
String appId = "your app id";
String devId = "your deveoper id";
String certId = "your Cert ID ";
apiAccount.setApplication(appId);
apiAccount.setDeveloper(devId);
apiAccount.setCertificate(certId);
apiContext.getApiCredential().setApiAccount(apiAccount);
return apiContext;
} //getApiContext
}
上面有4个参数需要获取
appId、devId、certId、ru_name
登录ebay deveopers program后,在首页->My Account->Application Keys页面下,可以看到AppId 、DevId、CertId
然后 点击上图中的User Token
上图中拿到RuName
3. 获得相应的use token
结果如下图所示:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<FetchTokenResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2018-05-30T08:26:55.783Z</Timestamp>
<Ack>Success</Ack>
<Version>1059</Version>
<Build>E1059_CORE_APISIGNIN_18690974_R1</Build>
<eBayAuthToken>xxxxxx</eBayAuthToken>
<HardExpirationTime>2019-11-21T08:25:46.000Z</HardExpirationTime>
</FetchTokenResponse>
</soapenv:Body>
</soapenv:Envelope>
参考资料:
https://blog.csdn.net/sunwukong54/article/details/12092187
http://developer.ebay.com/devzone/xml/docs/howto/tokens/gettingtokens.html
最新文章
- 2000条你应知的WPF小姿势 基础篇<;78-81 Dialog/Location/WPF设备无关性>;
- monggodb学习系列:1,mongodb入门
- [.net 面向对象编程基础] (1) 开篇
- SqlServer链接MySql操作步骤
- 自定义UICollectionViewLayout之瀑布流
- iOS 原生地图(MapKit、MKMapView)轨迹渐变
- Java条形码生成方案及二维码要点
- 【稳定婚姻问题】【HDU1435】【Stable Match】
- require.js(浅聊)
- php数组合并,反转,去重,添加元素等;
- Mac入门——快捷键
- ngnix 是什么
- Linux命令之常用篇
- gitlab备份
- ros的一些设置
- Angular CLI 安装和使用以及安装失败的解决方法
- Jersey入门三:创建一个JavaEE的Web项目
- Apache POI – Reading and Writing Excel file in Java
- 【Android】5.0 第5章 常用基本控件--本章示例主界面
- MongoDB-2:MongoDB添加、删除、修改