403 /禁止使用delphi应用程序访问我的Google日历

时间:2012-05-26 04:10:57

标签: delphi google-calendar-api indy10

有谁能告诉我为什么我无法访问我的日历信息?我得到了403禁止。

procedure TForm1.Button1Click(Sender: TObject);
var
  stringStream: TStringStream;
  slPost, slReply: TStringList;
  sPostResult: string;
begin
  slPost := TStringList.Create;
  slReply := TStringList.Create;
  try
    slPost.LineBreak := '&';
    slPost.Values['Email'] := 'me@gmail.com';
    slPost.Values['Passwd'] := 'pass';
    slPost.Values['service'] := 'cl';
    slPost.Values['source'] := 'company-program-version';

    stringStream := TStringStream.Create(slPost.Text);
    try
      IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
      sPostResult := IdHTTP1.Post('https://www.google.com/accounts/ClientLogin', stringStream);

      slReply.LineBreak:=#10;
      slReply.Text:=sPostResult;
      slReply.LineBreak:=#13#10;
      Memo1.Lines.Add(slReply.Text);
      Memo1.Lines.Add('response=' + IdHTTP1.ResponseText);

// 200 OK
      sPostResult := IdHTTP1.Post('https://www.google.com/accounts/ClientLogin', stringStream);

      IdHTTP1.Request.CustomHeaders.FoldLines:=false;
      IdHTTP1.Request.CustomHeaders.Clear;
      IdHTTP1.Request.CustomHeaders.Values['GData-Version']:='2.0';
      IdHTTP1.Request.CustomHeaders.Values['Authorization']:='GoogleLogin auth=' + slReply.Values['auth'];

(* custom headers:
      GData-Version: 2.0
      Authorization: GoogleLogin (line continues) auth=DQwhateverwhateverwhateverwhateverwhateverwhateverwhateverwhateverwhateverwhateverwhateverwhateverwhateverwhateverwhateverwhateverwhateverwhateverwhateverwhateverwhateverwhatever *)

      IdHTTP1.Request.ContentType := 'application/atom+xml';

// 403 Forbidden
      memo1.Lines.Add(IdHTTP1.Get('https://www.googleapis.com/calendar/v3/users/me/calendarList'));
    finally
      stringStream.Free;
    end;
  finally
    slPost.Free;
    slReply.Free;
  end;
end;
谢谢你! 熔点

1 个答案:

答案 0 :(得分:1)

经过一番阅读,我认为你需要处理Redirect。因此,如果响应是重定向,请获取新URL,使用新URL将授权重新附加到新请求标头。否则,您的重定向请求将缺少所需的授权,并且会给您403错误。