Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
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 tokensChilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Http,
Chilkat.StringBuilder,
Chilkat.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
jsonToken: TJsonObject;
refreshToken: string;
json: TJsonObject;
resp: THttpResponse;
sbResponseBody: TStringBuilder;
jResp: TJsonObject;
respStatusCode: Integer;
refresh_token: string;
access_token: string;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := THttp.Create;
// 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
jsonToken := TJsonObject.Create;
success := jsonToken.LoadFile('qa_data/tokens/wix.json');
if (success <> True) then
begin
WriteLn('Failed to load square.json');
Exit;
end;
// Get the "refresh_token"
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>
// }
json := TJsonObject.Create;
json.UpdateString('grant_type','refresh_token');
json.UpdateString('client_id','CLIENT_ID');
json.UpdateString('client_secret','CLIENT_SECRET');
json.UpdateString('refresh_token',refreshToken);
resp := THttpResponse.Create;
success := http.HttpJson('POST','https://www.wix.com/oauth/access',json,'application/json',resp);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
sbResponseBody := TStringBuilder.Create;
resp.GetBodySb(sbResponseBody);
jResp := TJsonObject.Create;
jResp.LoadSb(sbResponseBody);
jResp.EmitCompact := False;
WriteLn('Response Body:');
WriteLn(jResp.Emit());
respStatusCode := resp.StatusCode;
WriteLn('Response Status Code = ' + respStatusCode);
if (respStatusCode >= 400) then
begin
WriteLn('Response Header:');
WriteLn(resp.Header);
WriteLn('Failed.');
Exit;
end;
// Sample JSON response:
// {
// "refresh_token": "OAUTH2.eyJraWQ ... vnB4cQ",
// "access_token": "OAUTH2.eyJra ... la18lrw"
// }
refresh_token := jResp.StringOf('refresh_token');
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);
http.Free;
jsonToken.Free;
json.Free;
resp.Free;
sbResponseBody.Free;
jResp.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.