Why do I keep replying to 401 HTTP Request POST via Delphi Components?

Asked

Viewed 24 times

0

I’m trying to make a request with method post with a JSON in the content, with Calendar information that will be inserted, that is, integrating my application with the Google Calendar API. But it looks like I’m missing somewhere, the request returns 401 'Unauthorized' Since the Access Token is in the header. Somebody help me please!! I’ve been in this same problem for weeks.

My code

procedure TOAuth2TesterFrm.incluir_btnClick(Sender: TObject);
var
json,token: String;
begin
token:=AccessTokenEdt.Text;
json:=(
'{"kind": "calendar#calendarListEntry", "etag": "0", "id": "[email protected]", ' +
'"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;
    request.AddBody(json);
    client.AcceptEncoding:='UTF-8';
    client.ContentType:='application/json';
    request.Resource := 'calendars';
    request.Params.AddHeader('Authorization','Bearer '+token);
    request.Params.ParameterByName('Authorization').Options := [poDoNotEncode];


    request.Execute;
    ShowMessage(request.Response.StatusCode.ToString);
finally
  request.Free;
      response.Free;
      client.Free;

end;
  end;
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.