Delphi XE7 用indy开发微信公众平台(1)- 获取access_token、expires_in

微信公众平台官方的资料都是PHP的,这几天闲来无事,用Delphi实现了部分功能,希望能抛砖引玉,大家共同完善

加我微信:Leedege一起交流Delphi开发经验


开发平台:Delphi XE7

主要涉及JSON、XML的解析,SHA1加密,indy的IdHTTP、IdSSLIOHandlerSocketOpenSSL、IdHTTPServer控件

完全使用Delphi自带控件

获取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;
uses System.JSON;
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]);//AppID,AppSecret在你的公众平台上可以查到
  J := TJSONObject.ParseJSONValue(GetMethod(Url, 1)) as TJSONObject;
  try
    if J.Count > 0 then
    begin
      Access_Token := J.GetValue('access_token').Value;//这就是后面做什么都要用到的access_token,每天获取的次数是每天2000次,所以不能每次用都重新获取
      Expires_IN := J.GetValue('expires_in').Value.ToInteger;//access_token的过期时间,7200秒,所以要在过期前重新获取
    end;
  finally
    J.Free;
  end;
end;


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Devin.Lee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值