Last Updated
Viewed 66 Times
     

I need to set up a routine to check the token when using eBay sdk. From what I can see I need to use GetClientAlertsAuthTokenCall and execute it. I am not sure how I execute it. Or is there a better way to check for a token about to expire? Here is what I have so far for the check.

 public boolean isTokenExpiring() {
    try {
        GetClientAlertsAuthTokenCall tokenCall = new GetClientAlertsAuthTokenCall(getApiContext());
        tokenCall.execute(null);
        Calendar expireCalendar=tokenCall.getReturnedHardExpirationTime();
        Calendar currentCalendar=Calendar.getInstance();
    } catch (Exception e) {
        return false;
    }

    return false;
}

I have an auth_token of eBay, I want to get the expiration date of this token.

I know its duplicate question, but no answer was provided for that question: https://stackoverflow.com/questions/23474204/how-to-bring-ebay-token-expiry-date-using-existing-ebay-token

I'm trying to obtain an eBay OAuth access & refresh token but keep getting a 401 Unauthorized response. I've been through every scrap of documentation on this and tried just about everything but no joy.

I've been through the user permission flow, granted access to my application and have the authorize code - I'm manually pasting this into my code for now and have tried it both URL encoded and URL decoded but the same result.

I'm not sure if the problem lies with my java code or one of the values in my eBay developer account. Any ideas or pointers would be most welcome.

public int initialiseToken(String clientID, String clientSecret, String ruName)
{
    int responseCode = 0;
    try
    {
            String urlString = "https://api.ebay.com/identity/v1/oauth2/token";
            String clientCredentials = clientID + ":" + clientSecret;
            // base64 encode credentials
            byte[] base64clientCredentials = Base64.encodeBase64(clientCredentials.getBytes());

            // below authCode obtained from URI redirect following eBay auth sign-in 
            String authCodeURLEncoded = "v%5E1.1%23i%5E1%23I%5E3%23f.....xI0VeMjYw";
            String authCodeURLDecoded = URLDecoder.decode(authCodeURLEncoded, "UTF-8");

            URL url = new URL(urlString);
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Authorization", "Basic " + base64clientCredentials);
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Accept-Charset", "utf8");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            conn.setRequestProperty("grant_type", "authorization_code");
            conn.setRequestProperty("redirect_uri", ruName);
            conn.setRequestProperty("code", authCodeURLDecoded);         // have tried both encoded & decoded versions

            String msg;
            if (conn.getResponseCode() != 200) 
            {
                    responseCode = conn.getResponseCode();
                    msg = conn.getResponseMessage();
            }
            else
            {
                    responseCode = conn.getResponseCode();
                    msg = conn.getResponseMessage();
                    BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
                    String line = br.readLine();
                    parseResult(line);
            }
    }
    catch (MalformedURLException e) 
    {
            e.printStackTrace();
    } 
    catch (IOException e) 
    {
            e.printStackTrace();
    }
    catch (Exception e) 
    {
            e.printStackTrace();
    }
    return responseCode;
}

I'm trying to intergrate ebay's order API with my application. I have used following code to perform that.

public class Order {

    public static void getOrders() throws ApiException, SdkException, Exception {


        ApiContext apiContext = new ApiContext();
        ApiCredential cred = apiContext.getApiCredential();
        //Set Auth Token and server URL for sandbox
        cred.seteBayToken("v^1.1#i^1#r^0#p^1#I^3#f^0#t^H4sIAAAAAAAAAOVXa2wUVRTudreFggWJBuQRsw6GgM3MzmNnOzth124ppBX6gF0rrSi5M3OnHdmdGefedXcT1Fpj44+S+EPQCIYmFSNGDFETpUqC4SEJD43WKJGgCYFgNNWgEdRovLNdyrYqRVgNifNncs8599zvfOec+2B7Kqvu6mvsu1DtmVI+0MP2lHs83HS2qrKiZoa3fF5FGVtk4BnoubPH1+s9txSBVNKW10BkWyaC/mwqaSI5L4xQaceULYAMJJsgBZGMVTkea14l8wwr246FLdVKUv6mhggFQVjQFI7TVRUKYSlMpOYlnwkrQgVDgNd5EOYkDbKKWkv0CKVhk4kwMHGE4lkuRHM8zQsJTpQFSRY5JixKnZS/HTrIsExiwrBUNA9Xzs91irBeGSpACDqYOKGiTbEV8dZYU8PylsTSQJGvaIGHOAY4jcaPllka9LeDZBpeeRmUt5bjacIBQlQgOrrCeKdy7BKYa4CfpzqkhhRJ4SRWF2u5oCCWhMoVlpMC+Mo4XImh0XreVIYmNnBuMkYJG8pDUMWFUQtx0dTgd3+r0y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l4+bM+V11ddtO8DuHvju+82DjicWnjo+88cuiur1TzcOZjs++GplR9uT+o8c+uuPgMxS/eHjBqY9Pv7hJaTx0/uuujnXPbpnd31I/ODx7NI1/AO3YK892DgAA");
        apiContext.setApiServerUrl("https://api.sandbox.ebay.com/wsapi");
        ApiLogging apiLogging = new ApiLogging();
        apiContext.setApiLogging(apiLogging);        

        // Set the site based on the seller - for UK sellers, set it to SiteCodeType.UK
        // for US, set to SiteCodeType.US
        apiContext.setSite(SiteCodeType.US); 

        // set detail level to ReturnAll
        DetailLevelCodeType[] detailLevels = new DetailLevelCodeType[]{DetailLevelCodeType.RETURN_ALL};

        // define CreateTimeFrom and CreateTimeTo filters
        // set "To" to the current time 
        Calendar to = new GregorianCalendar(TimeZone.getTimeZone("GMT")).getInstance();         
        Calendar from = (GregorianCalendar) to.clone();

        // In this example we set "From" to 30 mins from current time - you need to set it to the last time you made the call
        from.add(to.MINUTE,-30);        

        GetOrdersCall getOrders = new GetOrdersCall(apiContext);
        getOrders.setDetailLevel(detailLevels);
        getOrders.setCreateTimeFrom(from);
        getOrders.setCreateTimeTo(to);

        OrderType[] orders = getOrders.getOrders();
        System.out.println("Finished " + orders.length);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            getOrders();
        } catch (ApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SdkException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

I got the token using following way.

1) Register a test account with ebay sandbox

2) Login to ebay developer account

3) My user account > Auth tokens for ebay > Get oAuth ApplicationToken

When I copy that token to my program and executing I got this following exception.

com.ebay.sdk.SdkSoapException: Validation of the authentication token in API request failed.
    at com.ebay.sdk.SdkSoapException.fromSOAPFaultException(Unknown Source)
    at com.ebay.sdk.ApiCall.executeByApiName(Unknown Source)
    at com.ebay.sdk.ApiCall.execute(Unknown Source)
    at com.ebay.sdk.call.GetOrdersCall.getOrders(GetOrdersCall.java:231)
    at ebayHelloWorld.Order.getOrders(Order.java:50)
    at ebayHelloWorld.Order.main(Order.java:56)

Could anybody please suggest me where I am making mistake here. Quicker responses more welcomed. Thanks

Similar Question 6 (1 solutions) : Ebay Finding API

Similar Question 9 (1 solutions) : Retrofit + OkHTTP with EBay's Inventory API

cc