Question

I have trouble to create an event in google , I'm using google manuals that are in your same website , when running the code

    CreateXML : = TStringList.Create ;
  CreateXML.Add (
  ' <? xml version = encoding = '' 1.0'' '' UTF- 8'' > ' # 13 # 10 +
  '< entry xmlns ='' '' ttp://www.w3.org/2005/Atom ' # 13 # 10 +
    ' xmlns: gd = ttp://schemas.google.com/g/2005 '' ''> ' # 13 # 10 +
  '< category scheme = '' '' ttp://schemas.google.com/g/2005 # kind ' # 13 # 10 +
    ' ttp://schemas.google.com/g/2005 # term = '' event ''> < / category> ' # 13 # 10 +
  ' <title> type=''text''> ' + title + ' < / title> ' # 13 # 10 +
  ' <content type=''text''> ' + content + ' < / content> ' # 13 # 10 +
  '< gd : transparency ' # 13 # 10 +
    ' ttp://schemas.google.com/g/2005 # value = '' '' event.opaque >' # 13 # 10 +
  '< / gd : transparency >' # 13 # 10 +
  '< gd : eventStatus ' # 13 # 10 +
    ' ttp://schemas.google.com/g/2005 # value = '' '' event.confirmed >' # 13 # 10 +
  '< / gd : eventStatus >' # 13 # 10 +
  ' <gd:where '''> valueString=''' + location + < / gd : where >' # 13 # 10 +
  '< gd : when startTime = ''' + '''' + EventStartTime # 13 # 10 +
    ' endTime ='' ' + eventEndTime + '' ' > < / gd : when> ' # 13 # 10 +
  '< / entry> ');

  slPost : = TStringList.Create ;
  slReply : = TStringList.Create ;

  slPost.Values ​​[' accountType '] : = ' HOSTED_OR_GOOGLE ';
  slPost.Values ​​[' email ' ] = email ;
  slPost.Values ​​[' Passwd ' ] = pass ;
  slPost.Values ​​[' service' ] : = ' cl ';
  slPost.Values ​​[' source' ] = '123 ';

  idHTTP1.IOHandler : = IdSSLIOHandlerSocketOpenSSL1 ;
  idHTTP1.Request.ContentType : = ' application / x -www -form- urlencoded ';
  Responsetxt : = idHTTP1.Post ( ' https://www.google.com/accounts/ClientLogin ' SlPost ) ;

  slReply.Delimiter : = # 10;
  slReply.DelimitedText : = Responsetxt ;
  FAuthID : = slReply.Values ​​[' Auth '] ;

  if Length ( FAuthID ) > 0 then
  Begin
  try

      URL : = ' https://www.google.com/calendar/feeds/ ' + NormalizeField (email) + '/ private / full ';

      idHTTP2.IOHandler : = IdSSLIOHandlerSocketOpenSSL1 ;
  IdHTTP2.Request.Connection : = ' Keep- Alive ';
      idHTTP2.Request.ContentType : = ' application / atom + xml' ;
      idHTTP2.Request.CustomHeaders.Values ​​[' GData - Version' ] = '2 .0 ' ;
      idHTTP2.Request.CustomHeaders.Values ​​[ 'Authorization' ] = (' GoogleLogin Auth =' + FAuthID );
    try
      Responsetxt : = idHTTP2.Post (URL , CreateXML );
    except
        on E: EIdHTTPProtocolException do
    begin

 

The response is text of IdHTTP2

the answer is in the header but GsessionID http:/302 variable does not appear, so I can not add it to header and only appears idhttp variable: SecureLocation and variable T

Please I hope you can help me with this problem

Thank you very much for your time.

Us Google Translator

Was it helpful?

Solution

Bad formatting aside (which I have to assume is NOT in your actual code), one problem your code has is that you are posting the XML as a TStringList. That will not work, as TIdHTTP will encode the XML in a manner that breaks the XML. You need to post the XML as a TStream instead so TIdHTTP will post it as-is without encoding it.

Try this:

  sXML := '<?xml version="1.0" encoding="UTF-8"?>'#13#10 +
          '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005">'#13#10 +
          '<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/g/2005#event"/>'#13#10 +
          '<title type="text">' + title + '</title>'#13#10 +
          '<content type="text">' + content + '</content>'#13#10 +
          '<gd:transparency value="http://schemas.google.com/g/2005#event.opaque"/>'#13#10 +
          '<gd:eventStatus value="http://schemas.google.com/g/2005#event.confirmed"/>'#13#10 +
          '<gd:where valueString="' + location + '"/>'#13#10 +
          '<gd:when startTime="' + EventStartTime + '" endTime="' + eventEndTime + '"/>'#13#10 +
          '</entry>';

  slPost := TStringList.Create;
  try    
    slPost.Values['accountType'] := 'HOSTED_OR_GOOGLE';
    slPost.Values​['email'] := email;
    slPost.Values​​['Passwd'] := pass;
    slPost.Values​​['service'] := 'cl';
    slPost.Values​​['source'] := '123';

    idHTTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL1 ;
    idHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
    Responsetxt := idHTTP1.Post('https://www.google.com/accounts/ClientLogin', slPost);
  finally
    slPost.Free;
  end;

  slReply := TStringList.Create;
  try
    slReply.Delimiter := #10;
    slReply.DelimitedText := Responsetxt;
    FAuthID := slReply.Values['Auth'];
  finally
    slReply.Free;
  end;

  if Length(FAuthID) > 0 then
  begin
    try
      URL := 'https://www.google.com/calendar/feeds/' + NormalizeField(email) + '/private/full';

      try
        // you did not say which version of Delphi or Indy
        // you are using, so I am taking some guesses...
        {$IFDEF UNICODE}
        CreateXML := TStringStream.Create(sXML, TEncoding.UTF8);
        {$ELSE}
        CreateXML : = TMemoryStream.Create;
        WriteStringToStream(CreateXML, sXML, enUTF8);
        CreateXML.Position := 0;
        {$ENDIF}

        try
          idHTTP2.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
          idHTTP2.Request.Connection := 'Keep-Alive';
          idHTTP2.Request.ContentType := 'application/atom+xml';
          idHTTP2.Request.CustomHeaders.Values['GData-Version'] := '2.0';
          idHTTP2.Request.CustomHeaders.Values​​['Authorization'] := 'GoogleLogin Auth=" + FAuthID;

          Responsetxt := idHTTP2.Post(URL, CreateXML);
        finally
          CreateXML.Free;
        end;
      except
        on E: EIdHTTPProtocolException do
        begin
          ...
        end;
      end;

With that said, you should consider creating your XML using an actual XML library, eg:

uses
  ..., XmlIntf, XmlDoc;

var
  XMLDoc: IXMLDocument;
  Entry: IXMLNode;
  Node: IXMLNode;
begin
  ...

  XMLDoc := NewXMLDocument;

  Entry := XMLDoc.AddChild('entry', 'http://www.w3.org/2005/Atom');
  Entry.DeclareNamespace('gd', 'http://schemas.google.com/g/2005');

  Node := Entry.AddChild('category');
  Node.Attribute['scheme'] := 'http://schemas.google.com/g/2005#kind';
  Node.Attribute['term'] := 'http://schemas.google.com/g/2005#event';

  Node := Entry.AddChild('title');
  Node.Attribute['type'] := 'text';
  Node.Text := title;

  Node := Entry.AddChild('content');
  Node.Attribute['type'] := 'text';
  Node.Text := content;

  Node := Entry.AddChild('gd:transparency');
  Node.Attribute['value'] := 'http://schemas.google.com/g/2005#event.opaque';

  Node := Entry.AddChild('gd:eventStatus');
  Node.Attribute['value'] := 'http://schemas.google.com/g/2005#event.confirmed';

  Node := Entry.AddChild('gd:where');
  Node.Attribute['valueString'] := location;

  Node := Entry.AddChild('gd:when');
  Node.Attribute['startTime'] := EventStartTime;
  Node.Attribute['endTime'] := eventEndTime;

  CreateXML := TMemoryStream.Create;
  try
    XMLDoc.SaveToStream(CreateXML);
    CreateXML.Position := 0;
    ...
    Responsetxt := idHTTP2.Post(URL, CreateXML);
  finally
    CreateXML.Free;
  end;

  ...
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top