1. 程式人生 > 其它 >delphi呼叫百度API獲取token

delphi呼叫百度API獲取token

procedure TForm1.btn_getTokenClick(Sender: TObject);
var
  sendData: string;
  url: string;
  ss: TStringStream;
  jsonData: isuperobject;
begin
  url := 'https://aip.baidubce.com/oauth/2.0/token';
  sendData := 'grant_type=client_credentials&client_id=' + Self.cxTextEdit_clientID.Text + '&client_secret=
' + Self.cxTextEdit_clientSecret.Text; try ss := TStringStream.Create('', TEncoding.UTF8); ss.WriteString(sendData); self.NetHTTPClient1.Accept := 'text/html, application/xhtml+xml, */*'; self.NetHTTPClient1.ContentType := 'application/x-www-form-urlencoded'; //application/json self.NetHTTPClient1.AcceptEncoding := '
UTF-8'; ss.Position := 0; jsonData := SO(NetHTTPClient1.Post(url, ss).ContentAsString(tencoding.UTF8)); if Assigned(jsonData['access_token']) then begin cxMemo1.Clear; cxMemo1.Lines.Add(jsonData['access_token'].AsString); access_token := jsonData['access_token'].AsString;
end; finally ss.Free; end; end;
client_id和client_secret 是你建立百度應用的時候生成的