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;