`
jiangduxi
  • 浏览: 445242 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

eBay SDK FetchTokenCall 的使用及其思考

 
阅读更多
大致需求:
 
引用
之前需求是ebay Account 和ebay Developer账户是一对一的进行绑定,然后将产生的Token直接保存至数据库中。现在需求则是需要通过FetchToken来获取产生新的token。



知识点预备
引用
查下ebay 相关文档 GetToken
Call 文档 FetchToken

实现步骤:
  • 登入进去ebay developer,找到Application settings
  • 选择 Select an environment 和 Select a key set
  • 点击Customize the eBay User Consent Form
  • 点击'Generate an RuName for Your Application'.

以上就是在ebay developer中设置



下面看看代码
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
引用

The end user has not completed Auth & Auth sign in flow

而这个exception出现就是由于sessionID通过代码获取而没有得到授权。
因此这里需要做如下处理:
在你获取到RunName和SessionID后,你必须组装
引用
https://signin.ebay.com/ws/eBayISAPI.dll?SignIn&RuName=XXX&SessID=XXX

然后打开一个用户需要输入用户名和密码的页面,进入后点击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 来使用
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics