ebay 如何获取用户token

Posted 梦见舟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ebay 如何获取用户token相关的知识,希望对你有一定的参考价值。

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

 

以上是关于ebay 如何获取用户token的主要内容,如果未能解决你的问题,请参考以下文章

关于ebay平台接口(php)对接示例

如何从 eBay 交易中获取卖家的 PayPal 账户

实现诸如 FourSquare 或 Ebay 之类的 Android 搜索过滤器

如何通过 eBay API 获取 PayPal TransactionID

Facebook access_token:用户接受我的应用后如何获取?

Express实战 - 应用案例- realworld-API - 路由设计 - mongoose - 数据验证 - 密码加密 - 登录接口 - 身份认证 - token - 增删改查API(代码片段