eBay SDK FetchTokenCall 的使用及其思考

[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]


下面看看代码

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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值