2

谁能告诉我为什么我无法访问我的日历信息?我被 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;

谢谢你!mp

4

1 回答 1

1

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

于 2012-05-26T07:03:19.430 回答