在Delphi中将访问令牌附加到谷歌REST请求的位置?

时间:2018-04-10 07:53:34

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

我正在尝试通过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,parseError。

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

1 个答案:

答案 0 :(得分:2)

自从我使用它以来,我对Delphi的帮助已经很多了,但是在添加访问令牌方面你有两个选择。

首先,只需将其作为参数添加到基本网址

Authorization : Bearer cn389ncoiwuencr

第二个选项是将请求作为授权标头发送给您的承载令牌。

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

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

function mainViewModel() {
var self = this;

    self.model = new function () {
        var model = this;
        model.myColor = ko.observable([{
            MainColor: '#0080C0',
            SecondaryColor: '#001111'
        }]);
    }

/* applyBindings is written in another method */

    self.domUtils = new function () {
        var domUtils = this;

        domUtils.initColorPicker = function (selector) {
            $('#' + selector + ' button').colpick({
                colorScheme: 'light',
                layout: 'rgbhex',
                submit: 0,
                color: function () {
                    if (selector == 'MainColor')
                        self.model.myColor()[0].MainColor;
                    else
                        self.model.myColor()[0].SecondaryColor;
                },
                onChange: function (hsb, hex, rgb, el) {
                    if (selector == 'MainColor')
                        self.model.myColor()[0].MainColor = '#' + hex;
                    else
                        self.model.myColor()[0].SecondaryColor = '#' + hex;                   
                 },  

                onShow: function (elem) {
                    var top = parseFloat($(elem).css('top').replace('px', ''));
                    var availableHeight = $(window).height();
                    if (top + $(elem).height() > availableHeight) {
                        $(elem).css('top', (availableHeight - $(elem).height() - 20) + 'px');
                    }
                }
            });
        };

        domUtils.initColorPickers = function () {
            domUtils.initColorPicker('MainColor');
            domUtils.initColorPicker('SecondaryColor');
        };
}