Sample code for 30+ languages & platforms
Java

Refresh WiX Access Token

See more WiX Examples

Request a new access token each time you call a WiX API. Use the refresh token together with your secret key, to request refresh tokens

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        // Implements the following CURL command:

        // curl -X POST \
        //   https://www.wix.com/oauth/access \
        //   -H 'Content-Type: application/json' \
        //   -d '{
        //     "grant_type": "refresh_token",
        //     "client_id": <CLIENT_ID>,
        //     "client_secret": <CLIENT_SECRET>,
        //     "refresh_token": <REFRESH_TOKEN>
        // }'

        // It is assumed we previously obtained an OAuth2 access token.
        // This example loads the JSON access token file
        // saved by this example: Get WiX OAuth2 Access Token

        JsonObject jsonToken = new JsonObject();
        try {
            jsonToken.loadFile("qa_data/tokens/wix.json");
        } catch (ChilkatException e) {
            System.out.println("Failed to load square.json");
            return;
        }

        // Get the "refresh_token"
        String refreshToken = jsonToken.stringOf("refresh_token");

        // The following JSON is sent in the request body.

        // {
        //   "grant_type": "refresh_token",
        //   "client_id": <APP_ID>,
        //   "client_secret": <APP_SECRET>,
        //   "refresh_token": <REFRESH_TOKEN>
        // }

        JsonObject json = new JsonObject();
        json.updateString("grant_type", "refresh_token");
        json.updateString("client_id", "CLIENT_ID");
        json.updateString("client_secret", "CLIENT_SECRET");
        json.updateString("refresh_token", refreshToken);

        HttpResponse resp = new HttpResponse();
        try {
            http.httpJson("POST", "https://www.wix.com/oauth/access", json, "application/json", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        StringBuilder sbResponseBody = new StringBuilder();
        resp.getBodySb(sbResponseBody);
        JsonObject jResp = new JsonObject();
        jResp.loadSb(sbResponseBody);
        jResp.setEmitCompact(false);

        System.out.println("Response Body:");
        System.out.println(jResp.emit());

        int respStatusCode = resp.getStatusCode();
        System.out.println("Response Status Code = " + respStatusCode);
        if (respStatusCode >= 400) {
            System.out.println("Response Header:");
            System.out.println(resp.getHeader());
            System.out.println("Failed.");
            return;
        }

        // Sample JSON response:

        // {
        //   "refresh_token": "OAUTH2.eyJraWQ ... vnB4cQ",
        //   "access_token": "OAUTH2.eyJra ... la18lrw"
        // }

        String refresh_token = jResp.stringOf("refresh_token");
        String access_token = jResp.stringOf("access_token");

        // Save the new JSON access token response to a file.
        sbResponseBody.writeFile("qa_data/tokens/wix.json", "utf-8", false);
    }
}