[size=medium][color=blue] 大致需求:
[quote]之前需求是ebay Account 和ebay Developer账户是一对一的进行绑定,然后将产生的Token直接保存至数据库中。现在需求则是需要通过FetchToken来获取产生新的token。[/quote]
[/color][/size]
[size=medium][color=red]知识点预备
[quote] 查下ebay 相关文档 [url=http://developer.ebay.com/DevZone/xml/docs/HowTo/Tokens/GettingTokens.html]GetToken[/url]
Call 文档 [url=http://developer.ebay.com/DevZone/XML/docs/Reference/ebay/FetchToken.html]FetchToken[/url]
[/quote]
实现步骤:
[list]
[*] 登入进去ebay developer,找到Application settings
[*] 选择 Select an environment 和 Select a key set
[*] 点击Customize the eBay User Consent Form
[*] 点击'Generate an RuName for Your Application'.
[/list]
以上就是在ebay developer中设置
[/color][/size]
下面看看代码
以上的代码还不能使用,看起来好像符合。但是在实际运行中,会出现以下Exception
[quote]
The end user has not completed Auth & Auth sign in flow
[/quote]
[size=medium]而这个exception出现就是由于sessionID通过代码获取而没有得到授权。
因此这里需要做如下处理:
在你获取到RunName和SessionID后,你必须组装
[quote]https://signin.ebay.com/ws/eBayISAPI.dll?SignIn&RuName=XXX&SessID=XXX
[/quote]
然后打开一个用户需要输入用户名和密码的页面,进入后点击I gree。就实现了授权,这时候你得到的sessionID就授权了。就可以通过FetchToken 得到token了。
还有一种方式是通过在ebay developer中配置得到授权的sessionID。那就是
在你产生了RunNames 后面,点击 show Details.可以看到Generate SessionID and Launch Consent Flow.点击后就到一个输入用户名和密码的页面,而这条URL中包含了RuName 和SessionID。只要用户输入正确的账户和密码,并且点击了I gree。那么URL中的SessionID也是授权了的。 用户可以复制URL的RunName 和 SessionID 来使用[/size]
[quote]之前需求是ebay Account 和ebay Developer账户是一对一的进行绑定,然后将产生的Token直接保存至数据库中。现在需求则是需要通过FetchToken来获取产生新的token。[/quote]
[/color][/size]
[size=medium][color=red]知识点预备
[quote] 查下ebay 相关文档 [url=http://developer.ebay.com/DevZone/xml/docs/HowTo/Tokens/GettingTokens.html]GetToken[/url]
Call 文档 [url=http://developer.ebay.com/DevZone/XML/docs/Reference/ebay/FetchToken.html]FetchToken[/url]
[/quote]
实现步骤:
[list]
[*] 登入进去ebay developer,找到Application settings
[*] 选择 Select an environment 和 Select a key set
[*] 点击Customize the eBay User Consent Form
[*] 点击'Generate an RuName for Your Application'.
[/list]
以上就是在ebay developer中设置
[/color][/size]
下面看看代码
public class getEbayToken {
private static String appID="XXXXX";
private static String devID="XXXXX";
private static String cert="XXXX";
private static String ruName="XXXXX";
private static String serverUrl="https://api.sandbox.ebay.com/wsapi";
//https://signin.ebay.com/ws/eBayISAPI.dll?SignIn&RuName=YourRuNameHere&SessID=YourSessionIDHere
public static void main(String[] args) throws ApiException, SdkException, Exception {
String sessionID =getSessionID();
getToken(sessionID);
}
/**
*
* @param sessionID
* @throws ApiException
* @throws SdkException
* @throws Exception
*/
public static void getToken(String sessionID) throws ApiException, SdkException, Exception{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
FetchTokenCall call = new FetchTokenCall();
ApiContext apiContext = new ApiContext();
apiContext.setApiServerUrl(serverUrl);
ApiCredential credential = new ApiCredential();
ApiAccount account = new ApiAccount();
account.setApplication(appID);
account.setDeveloper(devID);
account.setCertificate(cert);
credential.setApiAccount(account);
apiContext.setApiCredential(credential);
call.setApiContext(apiContext);
call.setSessionID(sessionID);
call.fetchToken();
System.out.println("ReturnToken: "+call.getReturnedToken());
Calendar ExpirationTime = call.getHardExpirationTime();
System.out.println("HardExpirationTime: "+sdf.format(ExpirationTime.getTime()));
}
/**
*
* @return
* @throws ApiException
* @throws SdkException
* @throws Exception
*/
public static String getSessionID() throws ApiException, SdkException, Exception{
ApiContext apiContext = new ApiContext();
apiContext.setApiServerUrl(serverUrl);
ApiCredential credential = new ApiCredential();
ApiAccount account = new ApiAccount();
account.setApplication(appID);
account.setDeveloper(devID);
account.setCertificate(cert);
credential.setApiAccount(account);
apiContext.setApiCredential(credential);
GetSessionIDCall call = new GetSessionIDCall();
call.setApiContext(apiContext);
call.setRuName(ruName);
String sessionID = call.getSessionID();
return sessionID;
}
}
以上的代码还不能使用,看起来好像符合。但是在实际运行中,会出现以下Exception
[quote]
The end user has not completed Auth & Auth sign in flow
[/quote]
[size=medium]而这个exception出现就是由于sessionID通过代码获取而没有得到授权。
因此这里需要做如下处理:
在你获取到RunName和SessionID后,你必须组装
[quote]https://signin.ebay.com/ws/eBayISAPI.dll?SignIn&RuName=XXX&SessID=XXX
[/quote]
然后打开一个用户需要输入用户名和密码的页面,进入后点击I gree。就实现了授权,这时候你得到的sessionID就授权了。就可以通过FetchToken 得到token了。
还有一种方式是通过在ebay developer中配置得到授权的sessionID。那就是
在你产生了RunNames 后面,点击 show Details.可以看到Generate SessionID and Launch Consent Flow.点击后就到一个输入用户名和密码的页面,而这条URL中包含了RuName 和SessionID。只要用户输入正确的账户和密码,并且点击了I gree。那么URL中的SessionID也是授权了的。 用户可以复制URL的RunName 和 SessionID 来使用[/size]