在Google日历中创建活动ClientLogin - Delphi语言

时间:2014-04-30 20:03:30

标签: delphi google-calendar-api idhttp

我无法在谷歌中创建活动,我在运行代码时使用的是同一网站上的谷歌手册

    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

回复是IdHTTP2的文本

答案是在标题中但GsessionID http:/ 302变量没有出现,所以我无法将其添加到标题中并且只显示idhttp变量:SecureLocation和变量T

我希望你能帮我解决这个问题

非常感谢你的时间。

我们谷歌翻译

1 个答案:

答案 0 :(得分:2)

格式不好(我必须假设它不在您的实际代码中),您的代码的一个问题是您将XML发布为TStringList。这将无效,因为TIdHTTP将以打破XML的方式对XML进行编码。您需要将XML作为TStream发布,以便TIdHTTP按原样发布,而不对其进行编码。

试试这个:

  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;

话虽如此,您应该考虑使用实际的XML库创建XML,例如:

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;