Insert Calendar API google via REST Delphi Response 401

Asked

Viewed 130 times

1

I am trying to use the REST components to make an HTTP request with the method POST to insert a calendar into the API, but my request is returning error "401 - Not authorized".

I’m trying to include the token in the request, but I don’t know if I’m doing it right. How can I fix this?

My current code

procedure TOAuth2TesterFrm.incluir_btnClick(Sender: TObject);
var
json,outro_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"}]}}');
outro_json:='{"summary": "Batata"}';
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(outro_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;
  • The header Authorization is being sent and rejected (invalid token) or is not being sent?

  • I believe it is not being sent because it is a valid token. Is that in this trial only has Insert, but in the other part of the app I log in with google and resgato the token

  • Did you test this token by sending a request by Postman or Insomnia (for example) to make sure you got the correct token? I don’t know Delphi, but that detail is important

  • I’ll try to use the Postman, but I believe it’s the right token

  • You were right, Insomnia also gave 401... So the token is invalid... And now, I need a valid token :(

  • Even if I generate several different tokens and try to submit, the request returns me 401. It can return this for some other reason besides invalid token??

Show 1 more comment
No answers

Browser other questions tagged

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