Sample code for 30+ languages & platforms
Android™

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 Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkHttp http = new CkHttp();

    // 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

    CkJsonObject jsonToken = new CkJsonObject();
    success = jsonToken.LoadFile("qa_data/tokens/wix.json");
    if (success != true) {
        Log.i(TAG, "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>
    // }

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

    CkHttpResponse resp = new CkHttpResponse();
    success = http.HttpJson("POST","https://www.wix.com/oauth/access",json,"application/json",resp);
    if (success == false) {
        Log.i(TAG, http.lastErrorText());
        return;
        }

    CkStringBuilder sbResponseBody = new CkStringBuilder();
    resp.GetBodySb(sbResponseBody);
    CkJsonObject jResp = new CkJsonObject();
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    Log.i(TAG, "Response Body:");
    Log.i(TAG, jResp.emit());

    int respStatusCode = resp.get_StatusCode();
    Log.i(TAG, "Response Status Code = " + String.valueOf(respStatusCode));
    if (respStatusCode >= 400) {
        Log.i(TAG, "Response Header:");
        Log.i(TAG, resp.header());
        Log.i(TAG, "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);

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}