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

获取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;

 

原文地址:http://www.cnblogs.com/devinlee/p/4282498.html

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

上一篇:Delphi XE7 用indy开发微信公众平台(3)- 验证消息真实性


下一篇:看微信了解MySQL及相关IT技术