简体   繁体   English

使用Delphi在Google日历中创建事件-错误401

[英]Create event in Google Calendar with Delphi - Error 401

My program has to connect to Google Calendar and create an event but I am receiving an error with text "Token invalid - AuthSub token has wrong scope" 我的程序必须连接到Google日历并创建一个事件,但是我收到一条错误消息,内容为“令牌无效-AuthSub令牌的作用域错误”

The URL is http://www.google.com/calendar/feeds/default/owncalendars/full 网址为http://www.google.com/calendar/feeds/default/owncalendars/full

This is my code: 这是我的代码:

Begin
  createXML := '<?xml version=''1.0'' encoding=''UTF-8''?>'#13#10 +
                  '<entry xmlns=''http://www.w3.org/2005/Atom'''#13#10 +
                  'xmlns:gd=''http://schemas.google.com/g/2005''>'#13#10 +
                  '<category scheme=''http://schemas.google.com/g/2005#kind'''#13#10 +
                  'term=''http://schemas.google.com/g/2005#event''></category>'#13#10 +
                  '<title type=''text''>' + title + '</title>'#13#10 +
                  '<content type=''text''>' + content + '</content>'#13#10 +
                  '<gd:transparency'#13#10 +
                  'value=''http://schemas.google.com/g/2005#event.opaque''>'#13#10 +
                  '</gd:transparency>'#13#10 +
                  '<gd:eventStatus'#13#10 +
                  'value=''http://schemas.google.com/g/2005#event.confirmed''>'#13#10 +
                  '</gd:eventStatus>'#13#10 +
                  '<gd:where valueString=''' + location + '''></gd:where>'#13#10 +
                  '<gd:when startTime=''' + EventStartTime + ''''#13#10 +
                  'endTime=''' + EventEndTime + '''></gd:when>'#13#10 +
                  '</entry>';

  HTTP:=THTTPSend.Create;
  url:=Edit3.Text;
  WriteStrToStream(HTTP.Document,EncodeURL(createXML));
  HTTP.Headers.Add('Authorization: OAuth '+OAuth.Access_token);
  HTTP.Headers.Add('GData-Version: 2');
  HTTP.MimeType:=('application/atom+xml');
  if HTTP.HTTPMethod('POST',url) then
  XML:=HTTP.Document;
  Memo2.Lines.LoadFromStream(XML);
End;

This is my code for OAuth Connection... 这是我的OAuth连接代码...

 Const
    client_id=%s&client_secret=%s&code=%s&redirect_uri=%s&grant_type=authorization_‌​‌​code'; 

.....

        procedure TForm2.Button4Click(Sender: TObject); var HTTP:THTTPSend; 
        begin HTTP:=THTTPSend.Create; 
        try 
        HTTP.Headers.Add('Authorization: OAuth '+OAuth.Access_token); 
        HTTP.Headers.Add('GData-Version: 2'); 
        if       HTTP.HTTPMethod('GET','google.com:443/calendar/feeds/default/allcalendars/full') then 
        XML:=HTTP.Document; 
        End;

Thank you very much for your time. 非常感谢您的宝贵时间。

User: Google translate 用户:Google翻译

Here you can read the protocol including what Authorization headers does it need. 在这里,您可以阅读协议,包括所需的授权标头。 Even with the code you added it's still unclear how the OAuth object is built. 即使添加了代码,也仍然不清楚OAuth对象是如何构建的。

However, I don't recommend you to continue using the GData API as it's being deprecated. 但是,不建议您继续使用GData API,因为它已被弃用。 Consider using the JSON-based v3 version along with the new authentication method. 考虑将基于JSON的v3版本与新的身份验证方法一起使用。 The API specification and usage is also new, so you will need to invest some time. API规范和用法也是新的,因此您需要花费一些时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM