我不知道如何通过Delphi在Google日历API中插入日历

我不知道如何通过Delphi在Google日历API中插入日历,delphi,google-calendar-api,Delphi,Google Calendar Api,我正在使用REST组件来请求Google日历API并插入日历。 但我不知道现在该怎么办,因为请求-响应=401未经授权。我认为使用标题作为凭证,但我确实这样做了吗? 请帮助我,我在Delphi的新程序员 这是我现在的代码 procedure TOAuth2TesterFrm.incluir_btnClick(Sender: TObject); var json,token: String; begin token:=AccessTokenEdt.Text; json:=( '{"

我正在使用REST组件来请求Google日历API并插入日历。 但我不知道现在该怎么办,因为请求-响应=401未经授权。我认为使用标题作为凭证,但我确实这样做了吗? 请帮助我,我在Delphi的新程序员

这是我现在的代码

    procedure TOAuth2TesterFrm.incluir_btnClick(Sender: TObject);
var
json,token: String;
begin
token:=AccessTokenEdt.Text;
json:=(
'{"kind": "calendar#calendarListEntry", "etag": "0", "id": "iago21092020@gmail.com", ' +
'"summary": "Calendario Outro", "description": "Descrição do evento", '   +
'{"date": "2020 August 11", "dateTime": "2020-08-11T17:35:36+03:00"}'+
'"timeZone": "Brasília-DF", "colorId": "15", "backgroundColor": "#9fc6e7", "foregroundColor": "#000000", ' +
'"selected": true, "accessRole": "owner", "primary": true, '+
 '"defaultReminders": [{"method": "popup", "minutes": 30}, {"method": "email", "minutes": 10}], '  +
'"notificationSettings": {"notifications": [{"method": "email", "type": "eventCreation"}, ' +
'{"method": "email", "type": "eventChange"}, {"method": "email", "type": "eventCancellation"}, ' +
'{"method": "email", "type": "eventResponse"}]}}');
try
    client.ResetToDefaults;
    request.ResetToDefaults;
    response.ResetToDefaults;
    client.BaseURL:='https://www.googleapis.com/calendar/v3/';
    response.ContentType := 'application/json';
    request.Method := TRESTRequestMethod.rmPOST;
    request.Body.ClearBody;
    client.AcceptEncoding:='UTF-8';
    client.ContentType:='application/json';
    request.Resource := 'calendars';
    client.Params.AddHeader('Authorization','Bearer '+token);
    client.Params.ParameterByName('Authorization').Options := [poDoNotEncode];
    request.AddBody(json);
    request.Execute;
    ShowMessage(request.Response.StatusCode.ToString);
finally
      request.Free;
      response.Free;
      client.Free;

end;
  end;

如果添加注释行,则会出现访问冲突错误

据我所知,您需要使用TRestClient.Params属性添加标题:

RestClient.Params.AddHeader(‘Autorization’, ‘Bearer ‘ + token);

如果仍然存在问题,您可能需要指定添加参数的“Options”属性:如果您的令牌已经是JWT,则添加选项
poDoNotEncode

请参见(它是c#,但解释了如何创建访问令牌-基本上是JWT),您需要添加类似“Authorization:Bearer”的头,我已经这样做了,只是因为IDHTTP不是正确的组件,我在您的代码中使用REST,您将头添加为“:Token”,但google声明您需要使用“:Bearer”。第二,您没有在headername/前缀和令牌之间添加空格。对不起,我更改了代码,没有更新代码,但非常感谢它帮助了我。如果您确实知道问题所在,请将其张贴在此处。我不知道问题所在,但我正在努力solve@M这条错误消息意味着它没有检测到授权头。你的设置仍然不正确。好吧,但我添加了一个标题,就像其他朋友说的