【问题标题】:delphi google calendar Api event insertdelphi谷歌日历Api事件插入
【发布时间】:2018-09-13 19:35:31
【问题描述】:

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

这是目前为止的代码:

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.
基本网址: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;,我会得到我所有现有事件的列表。

【问题讨论】:

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


    【解决方案1】:

    ":"无效的凭据"

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

    选项 2:

    尝试对用户进行身份验证,可能是访问令牌已过期,需要刷新

    【讨论】:

    • 你确定吗?我可以得到我所有事件的列表,我只是不知道如何插入事件
    • 这意味着你确定你没有混淆你的代码一个调用发送正确的凭据另一个做错了。
    • 我使用 RESTDemo 作为框架来测试代码。如果我不打电话给TestGoogleRestParams,我会得到事件列表。
    • 尝试选项 2 我的 Delphi 真的很生疏,试图通过您的代码进行挑选
    • 我在调用此代码之前确实获得了身份验证,并且令牌需要一个小时才能过期
    猜你喜欢
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多