Hello everyone, this article introduces the whole process of docking ele. me API, hoping to give others some reference to get started and avoid detdettions.

1) General process

  1. Register account: Developer account
  2. Create apps: Apps on the Ele. me platform
  3. Obtain token: The merchant authorizes the application to access the merchant’s resources on the Ele. me platform
  4. Access API: Ele. me provides a variety of APIS for use, it is recommended to use SDK to write code

2) Register an account

  1. Visit https://open.shop.ele.me/openapi account registered developers
  2. After registering an account, you need to submit enterprise materials for [qualification certification], and then you can access the interface after passing the certification

Registration link nothing much to say, as long as the material specification submitted to it, generally a day will pass the audit

3) Create an application

  1. While waiting for approval, we can create an [application], we can focus on the [sandbox environment], the rest can not be ignored. (I created [enterprise application])

  2. Click on the sandbox environment, these parameters are used

What is a sandbox environment? A: It’s a test environment, whatever you do.

4) the access token

  1. Getting a token is something you can only get after writing code and deploying it to the server. You just can’t do it locally. (An online server is required)
  2. The steps are as follows:
    • Behind the first to know about the SDK development platform to provide (visit shops information also suggested using the SDK to write code, convenient) https://open.shop.ele.me/openapi/documents/sdk

    • In the most direct way: Open the web page through THE URL (provided by Ele. me), enter the account number and password for the merchant, and redirect to callback_URL (which is the [callback address URL] configured in the application, with two new parameters code and state attached) after authorization. The parameter code will be included in the callback_URL. Is the most important parameter for token generation.

    • How to get the URL of the authorized web page? The appKey parameter is the key

    public static String getAuthUrl(boolean isSandbox, String appKey, String appSecret, String callbackUrl) {
        Config config = new Config(isSandbox, appKey, appSecret);
        OAuthClient client = new OAuthClient(config);
        
        String scope = "all"; // Interface requirements All String state ="xyz"; // Arbitrary value String authUrl = client.getauthurl (callbackUrl, scope, state);return authUrl;
    }
Copy the code
  • What does the authorized page of Ele. me look like?

  • CallbackUrl after successful authorization

Such as in your application configuration is http://42.18.18.18/eleme/callback/getTokenByCode

After the success of the authorization to jump to the url is http://42.18.18.18/eleme/callback/getTokenByCode? code=ae9522071bacb7b371d940c4348a78d5&state=xyz

The code in this is very important, and with the code, we can generate tokens.

  • The code to generate the token is very simple. The callbackUrl is the URL configured in the application.
    public static Token getToken(boolean isSandbox, String appKey, String appSecret, String callbackUrl, String code) {
        Config config = new Config(isSandbox, appKey, appSecret);
        
        OAuthClient client = new OAuthClient(config);
        
        return client.getTokenByCode(code, callbackUrl);
    }
Copy the code

Ok, here we are, the most important token is taken care of. With the token, we have free access to the API. (The effective management of token will not be introduced in this article)

What does the token object look like?

        Token token = new Token();
        token.setAccessToken("f8764e056d5ef9a9aee79bb5796895");
        token.setTokenType("Bearer"); token.setExpires(86400); // Sandbox tokens are valid for one day token.setRefreshToken("2b370d4315eba2115325f11785dc0");
Copy the code

5) to obtain API

Access the API through the SDK provided by Ele. me, for example, to obtain the name and address of the merchant

    public static void getShopInfo(boolean isSandbox, String appKey, String appSecret, Token token, int shopId) {
        try {
            Config config = new Config(isSandbox, appKey, appSecret);
            ShopService shopService = new ShopService(config, token);
            OShop shop = shopService.getShop(shopId);
            log.info("Store Name:"+shop.getName()+Address:"+shop.getAddressText()); } catch(Exception e) { e.printStackTrace(); }}Copy the code

Results:

6) summary

Due to the limited capacity of the small editor and the simple introduction of some contents in the document center, I spent most time on the acquisition of token in the whole process.

Effective management of tokens is also important in real product development.

Be clear about key, Secret, code, token, Callback_URL, store ID, store account password and other concepts.

Note: to install the PC version of the merchant platform, from the inside to open my shop (direct browser version of the open seems to be unable to open, indicating that the restaurant does not exist)