Delphi ActiveX
Delphi ActiveX
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 Delphi ActiveX Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
http: TChilkatHttp;
jsonToken: TChilkatJsonObject;
refreshToken: WideString;
json: TChilkatJsonObject;
resp: TChilkatHttpResponse;
sbResponseBody: TChilkatStringBuilder;
jResp: TChilkatJsonObject;
respStatusCode: Integer;
refresh_token: WideString;
access_token: WideString;
begin
success := 0;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := TChilkatHttp.Create(Self);
// 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 := TChilkatJsonObject.Create(Self);
success := jsonToken.LoadFile('qa_data/tokens/wix.json');
if (success <> 1) then
begin
Memo1.Lines.Add('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 := TChilkatJsonObject.Create(Self);
json.UpdateString('grant_type','refresh_token');
json.UpdateString('client_id','CLIENT_ID');
json.UpdateString('client_secret','CLIENT_SECRET');
json.UpdateString('refresh_token',refreshToken);
resp := TChilkatHttpResponse.Create(Self);
success := http.HttpJson('POST','https://www.wix.com/oauth/access',json.ControlInterface,'application/json',resp.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(http.LastErrorText);
Exit;
end;
sbResponseBody := TChilkatStringBuilder.Create(Self);
resp.GetBodySb(sbResponseBody.ControlInterface);
jResp := TChilkatJsonObject.Create(Self);
jResp.LoadSb(sbResponseBody.ControlInterface);
jResp.EmitCompact := 0;
Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(jResp.Emit());
respStatusCode := resp.StatusCode;
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
begin
Memo1.Lines.Add('Response Header:');
Memo1.Lines.Add(resp.Header);
Memo1.Lines.Add('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',0);
end;