开发工具:delphi7
indy10.6.2
https://download.csdn.net/download/3d173/88063420
json
https://download.csdn.net/download/3d173/88062881
1.获取令牌,令牌是20分钟更新一次 所以要有个定时器time1
const
appid=‘xxxxx’
AppSecret=‘xxxxx’
poi_id=‘xxxxx’(上面三个参数是前面获取的)
session:string 这是令牌
procedure TForm1.Timer1Timer(Sender: TObject);
var
url, s: string;
json: tjson;
begin
url := 'https://open.douyin.com/oauth/client_token/?client_key=' + app_key + '&client_secret=' + AppSecret + '&grant_type=client_credential';
self.IdHTTP1.Request.ContentType := 'multipart/form-data';
s := utf8toansi(Self.IdHTTP1.get(PChar(url), IndyTextEncoding_UTF8));
Json := TJson.Create();
Json.Parse(s);
session1 := Json['data'].AsObject['access_token'].AsString;//获取的令牌
json.Free;
end;
2.验券前准备
由于抖音的二维码扫描出来是一个短网址 http://douyin.com/eSEDDDW2
需要把他变成一个长网址 这里 我们使用 (输入直接输入数字请忽略此步)
WebBrowser1
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key = 13 then
begin
if length(self.Edit1.Text) > 10 then
begin
if pos('douyin', self.Edit1.Text) > 0 then
begin
self.WebBrowser1.Navigate(self.Edit1.Text);//此处是抖音扫描出来的码
end;
end;
self.Edit1.Text := '';
self.Edit1.SetFocus;
end;
end;
这里验券
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
var s:string;
begin
s := self.WebBrowser1.LocationURL;
s1 := copy(s, pos('object_id=', s) + 10, 188);//此处获得188位的抖音码
验券前命令
url1 := 'https://open.douyin.com/goodlife/v1/fulfilment/certificate/prepare/?access_token=' + session
+ '&encrypted_data=' + urlencode(s1);
self.IdHTTP1.Request.ContentType := 'application/json';
strStream := Tstringstream.Create('');
Self.IdHTTP1.get(url1, strStream); ;
s2 := utf8toansi(strStream.DataString);
strStream.Free;
使用jsons解析获得数据获取verify_token和encrypted_code
3.最后验券
url1 := 'https://open.douyin.com/goodlife/v1/fulfilment/certificate/verify/?access_token=' + session; //验券
self.IdHTTP1.Request.ContentType := 'application/json';
sendstr := '{"verify_token":"' + verify_token + '","encrypted_codes":["' + encrypted_code+ '"],"poi_id":poi_id}';poi_id就是门店id
RequestList := TStringStream.Create(sendstr);
try
Return := TStringStream.Create('');
Self.IdHTTP1.post(url1, RequestList, Return);
s := utf8toansi(Return.DataString);
memo2.Text := s;
if pos('履约成功', s) > 0 then
begin
//你的代码
end;
end;