Sample code for 30+ languages & platforms
Dart

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 Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

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

  final http = 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

  final jsonToken = CkJsonObject();
  try {
    jsonToken.loadFile('qa_data/tokens/wix.json');
  } on ChilkatException {
    print('Failed to load square.json');
    return;
  }

  // Get the "refresh_token"
  var 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>
  // }

  final json = CkJsonObject();
  json.updateString('grant_type', 'refresh_token');
  json.updateString('client_id', 'CLIENT_ID');
  json.updateString('client_secret', 'CLIENT_SECRET');
  json.updateString('refresh_token', refreshToken);

  final resp = CkHttpResponse();
  try {
    http.httpJson('POST', 'https://www.wix.com/oauth/access', json, 'application/json', resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final sbResponseBody = CkStringBuilder();
  resp.getBodySb(sbResponseBody);
  final jResp = CkJsonObject();
  jResp.loadSb(sbResponseBody);
  jResp.emitCompact = false;

  print('Response Body:');
  print(jResp.emit());

  final respStatusCode = resp.statusCode;
  print('Response Status Code = $respStatusCode');
  if (respStatusCode >= 400) {
    print('Response Header:');
    print(resp.header);
    print('Failed.');
    return;
  }

  // Sample JSON response:

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

  refreshToken = jResp.stringOf('refresh_token');
  final accessToken = jResp.stringOf('access_token');

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