获取access_token、expires_in
function GetMethod(HTTP: TIdHTTP; Url: String; Max: Integer): String; var RespData: TStringStream; begin RespData := TStringStream.Create(‘‘, TEncoding.UTF8); try try HTTP.Get(Url, RespData); HTTP.Request.Referer := Url; Result := RespData.DataString; except Dec(Max); if Max = 0 then begin Result := ‘‘; Exit; end; Result := GetMethod(Url, Max); end; finally FreeAndNil(RespData); end; end;
const TokenUrl = ‘https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s‘; procedure GetToken(AppID, AppSecret: String); var Url: string; J: TJSONObject; begin Url := Format(TokenUrl, [AppID, AppSecret]); J := TJSONObject.ParseJSONValue(GetMethod(Url, 1)) as TJSONObject; try if J.Count > 0 then begin Access_Token := J.GetValue(‘access_token‘).Value; Expires_IN := J.GetValue(‘expires_in‘).Value.ToInteger; end; finally J.Free; end; end;