ebay 如何获取用户token

1. 首先 配置环境加载依赖的ebay SDK

下载地址 https://go.developer.ebay.com/ebay-sdks

需要在本地仓库安装下面的jar

mvn install:install-file -Dfile=C:ebayebaysdkjava1055libebaycalls.jar -DgroupId=com.ebay -DartifactId=ebaycalls -Dpackaging=jar -DgeneratePom=true -Dversion=1.7.0
mvn install:install-file -Dfile=C:ebayebaysdkjava1055libebaysdkcore.jar -DgroupId=com.ebay -DartifactId=ebaysdkcore -Dpackaging=jar -DgeneratePom=true -Dversion=1.7.0
mvn install:install-file -Dfile=C:ebayebaysdkjava1055libhelper.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

原文地址:https://www.cnblogs.com/mengjianzhou/p/9111907.html