【问题标题】:Where to attach access token to google REST request in Delphi?Delphi 在哪里将访问令牌附加到谷歌 REST 请求?
【发布时间】:2018-04-10 07:53:33
【问题描述】:

我正在尝试通过 Delphi REST 控件将事件插入到我的 Google 日历中,但我不确定在哪里将访问令牌添加到我的请求中。
我的代码如下所示:

var
  evt : String;    
begin
  ResetRESTComponentsToDefaults;
  RESTClient.BaseURL := 'https://www.googleapis.com/calendar/v3';       
  RESTClient.Authenticator := OAuth2_GoogleCalendar;
  RESTRequest.Resource := 'calendars/primary/events'; 
  evt:='{"summary":"test","description":"test","id":"06824945162f4204bfdc041ae1bbae85","start":{"date":"2018-04-10"},"end":{"date":"2018-04-10"},"guestsCanInviteOthers":false,"visibility":"private"}'

  RESTRequest.AddParameter('access_token',OAuth2_GoogleTasks.AccessToken,pkHTTPHEADER);
  RESTRequest.Method := TRESTRequestMethod.rmPOST;
  RESTRequest.Body.Add(evt,ctAPPLICATION_JSON);

  RESTRequest.Execute;
end;

范围是https://www.googleapis.com/auth/calendar
如果我像这样发送它,我会出现这个错误:

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

?access_token={accessToken} 附加到我得到的网址的末尾
错误 400,解析错误。

我应该在哪里将访问令牌添加到请求中?

【问题讨论】:

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


    【解决方案1】:

    自从我使用 Delphi 多年以来,我帮不上什么忙,但在添加访问令牌方面,您有两种选择。

    首先是将其作为参数添加到基本 url 上

    ?access_token=TokenHere
    

    第二种选择是在您的请求中将其作为授权标头发送,即不记名令牌。

    Authorization : Bearer cn389ncoiwuencr
    

    经过一番谷歌搜索,我找到了this

    FIdHTTP.Request.CustomHeaders.FoldLines := False;
    FIdHTTP.Request.CustomHeaders.Add('Authorization:Bearer ' + txtToken.Text);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-18
      • 2021-06-23
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      • 2019-07-07
      相关资源
      最近更新 更多