delphi谷歌日历Api事件插入

时间:2018-04-04 09:38:23

标签: rest delphi google-api google-calendar-api google-oauth

我正在尝试使用delphi REST控件将事件插入到我的Google日历中。

这是到目前为止的代码:

procedure TForm1.TestGoogleRestParams;
var
  i: Integer;
  jsonObjEventResource,jsonObjStart,jsonObjEnd: TJSONObject;
begin
  try
    jsonObjEventResource:=TJSONObject.Create();
    jsonObjStart:=TJSONObject.Create();
    jsonObjEnd:=TJSONObject.Create();

    jsonObjEventResource.AddPair(TJSONPair.Create('summary','test'));
    jsonObjEventResource.AddPair(TJSONPair.Create('description','Testing'));
    jsonObjEventResource.AddPair(TJSONPair.Create('id',LowerCase('06824945162F4204BFDC041AE1BBAE85')));

    jsonObjStart.AddPair(TJSONPair.Create('date',FormatDateTime('yyyy-mm-dd',Now)));

    jsonObjEventResource.AddPair(TJSONPair.Create('start',jsonObjStart));

    jsonObjEnd.AddPair(TJSONPair.Create('date',FormatDateTime('yyyy-mm-dd',Now)));

    jsonObjEventResource.AddPair(TJSONPair.Create('end',jsonObjEnd));

    jsonObjEventResource.AddPair(TJSONPair.Create('guestsCanInviteOthers',TJSONBool. Create(false)));
    jsonObjEventResource.AddPair(TJSONPair.Create('visibility','private'));

    mem_Test.Lines.Add(TJson.Format(jsonObjEventResource));
    //mem_Test.Lines.Add(jsonObjEventResource.ToJSON);
    RESTRequest.Method := TRESTRequestMethod.rmPOST;
    RESTRequest.Body.ClearBody;
    RESTRequest.AddBody(jsonObjEventResource);
    RESTRequest.Execute;
  finally
    //jsonObjEventResource.Free;
    //jsonObjStart.Free;
    //jsonObjEnd.Free;
  end;
end;

我使用的范围是:https://www.googleapis.com/auth/calendar BaseURL:https://www.googleapis.com/calendar/v3
ResourceURI:日历/主要/事件

我确实获得了访问令牌和刷新令牌,但我无法发布请求。这是我收到的错误:

{
  "error":
  {
    "errors":
    [
            {
        "domain":"global",
        "reason":"required",
        "message":"Login Required",
        "locationType":"header",
        "location":"Authorization"
      }
    ]
,
    "code":401,
    "message":"Login Required"
  }
}

使用以下uri:https://www.googleapis.com/calendar/v3/calendars/primary/events

我该如何解决这个问题?

如果我不调用此方法,只需致电RESTRequest.Execute;,我会获得所有现有活动的清单。

1 个答案:

答案 0 :(得分:0)

  

“:”凭据无效“

表示您使用的客户端ID和客户端密码不正确。您应该从Google Developer Console下载新文件,然后重试。

选项2:

尝试验证用户可能是访问令牌已过期且需要刷新

相关问题