delphi - 当我尝试使用 Delphi 将文件上传到一个驱动器时出现错误请求错误

标签 delphi onedrive

我正在尝试使用 Delphi 中的 RestAPI 将文件上传到一个驱动器。但无法将文件上传到一个云端硬盘。对一个驱动器的身份验证成功,但上传文件时出错。以下是我收到的错误消息。

enter image description here

edt_OneDrive_AuthCode := '';
    edt_OneDrive_AccessToken := '';
    edt_OneDrive_RefreshToken := '';
    edt_OneDrive_ClientID:= '********'     ;
    edt_OneDrive_ClientSecret:='*********';
    edt_OneDrive_RedirectURI:='https://login.live.com/oauth20_desktop.srf';

    LURL := 'https://login.live.com/oauth20_authorize.srf';
    LURL := LURL + '?response_type=' + URIEncode('code');
    LURL := LURL + '&client_id=' + URIEncode(edt_OneDrive_ClientID);
    LURL := LURL + '&redirect_uri=' + URIEncode(edt_OneDrive_RedirectURI);
    LURL := LURL + '&scope=' + URIEncode('wl.basic wl.signin wl.skydrive wl.skydrive_update onedrive.readwrite');

   ///1.Get authorizationCode
     wv := Tfrm_OAuthWebForm.Create(self);
    try
      wv.OnTitleChanged := self.OAuth2_GoogleTasks_BrowserTitleChanged;
      wv.ShowModalWithURL(LURL);
    finally
      wv.Release;
    end;

    {ResetRESTComponentsToDefaults; }
    /// step #2: get the access-token using authorizationCode

   { ResetRESTComponentsToDefaults;}
    RESTClient:=TRESTClient.Create(nil);
    RESTClient.BaseURL := 'https://login.live.com/';     {' https://apis.live.net/v5.0/';  }

  RESTRequest := TRESTRequest.Create(nil);

    RESTRequest.Method := TRESTRequestMethod.rmPOST;
    RESTRequest.Resource := 'oauth20_token.srf';///https://login.live.com/oauth20_token.srf?pretty=false
    RESTRequest.Client:=   RESTClient;

    ///RESTRequest.Params.AddHeader('Content-Type', TRESTContentType.ctAPPLICATION_X_WWW_FORM_URLENCODED);
    ///RESTRequest.Params.AddHeader('Content-Type', 'application/x-www-form-urlencoded');
    ///RESTRequest.ContentType:= TRESTContentType.ctAPPLICATION_X_WWW_FORM_URLENCODED;
    /// RESTRequest.Params.AddHeader('Content-Type', TRESTContentType.ctAPPLICATION_X_WWW_FORM_URLENCODED);
     ///RESTRequest.Params.AddHeader('Content-Type', 'application/x-www-form-urlencoded');

    {RESTRequest.ContentType:=  'application/x-www-form-urlencoded';   }

    { _options.ClientId, _options.ClientSecret, _options.CallbackUrl, authorizationCode }
    RESTRequest.Params.AddItem('code', edt_OneDrive_AuthCode, TRESTRequestParameterKind.pkGETorPOST);
    RESTRequest.Params.AddItem('client_id', edt_OneDrive_ClientID, TRESTRequestParameterKind.pkGETorPOST);
    RESTRequest.Params.AddItem('client_secret', edt_OneDrive_ClientSecret, TRESTRequestParameterKind.pkGETorPOST);
    RESTRequest.Params.AddItem('redirect_uri',edt_OneDrive_RedirectURI , TRESTRequestParameterKind.pkGETorPOST);
    RESTRequest.Params.AddItem('grant_type', 'authorization_code', TRESTRequestParameterKind.pkGETorPOST);



    RESTRequest.Execute;

    OAuth2_OneDrive:=TOAuth2Authenticator.Create(nil);

    if RESTRequest.Response.GetSimpleValue('access_token', LToken) then
      OAuth2_OneDrive.AccessToken := LToken;
    if RESTRequest.Response.GetSimpleValue('refresh_token', LToken) then
      OAuth2_OneDrive.RefreshToken := LToken;

           {$IF DEFINED(MsWindows)}
      local_filename :=  'C:\Users\mahesh.daram\Desktop\a.txt';

     {$ENDIF}
//      RESTResponseDataSetAdapter.AutoUpdate := false;

      RESTRequest.Params.Clear;
      RESTRequest.ClearBody;
      RESTRequest.Method := TRestRequestMethod.rmPOST;




      //RESTClient.Authenticator:=OAuth2_OneDrive;
      ///
      RESTClient := TRESTClient.Create('https://api.onedrive.com/v1.0');

        RESTClient.Authenticator :=OAuth2_OneDrive ;

         ///
      //RESTClient.BaseURL :=     'https://api.onedrive.com/v1.0';
      RESTRequest.Resource:='/drive/items/root:/a.txt';
//      RESTRequest.Resource:='/drive/items/root:/a.txt:/content';
      RESTRequest.Client:= RESTClient;
      upload_stream := TFileStream.Create(local_filename,fmOpenRead);
      upload_stream.Position := 0;
                             //Set Content-Type to text/plain
  //Set Request Body to FileStream
    RESTRequest.ClearBody;


        RESTRequest.Addbody(upload_stream, TRESTContentType.ctAPPLICATION_OCTET_STREAM);


                           RESTRequest.Execute;//Getting exception here

我们在最后一行(RESTRequest.Execute)遇到异常。任何机构都可以建议如何解决这个问题吗?

最佳答案

要与 OneDrive 上的文件交互,您应该使用 https://api.onedrive.com 基本 URL。

要执行上传,您需要构造一个如下所示的 url,然后将文件内容放入该位置。还有更多上传选项可能更适合您的应用程序场景,了解它们 here .

https://api.onedrive.com/v1.0/drive/root:/{parent-path}/{filename}:/content

OneDrive API 记录于 https://dev.onedrive.com/适用于与 OneDrive 中的元数据交互以及上传和下载的所有场景。

关于delphi - 当我尝试使用 Delphi 将文件上传到一个驱动器时出现错误请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36895578/

相关文章:

sharepoint - 使用 "sourcedoc"ID 从 Microsoft Graph API 下载 SharePoint 文件

c# - 如何在 .NET 中创建带有脱机文件(类似于 OneDrive)的虚拟文件系统?

delphi - Delphi中如何检查字符串是否包含子字符串?

database - 如何使用数据感知控件 "correctly"?

用于 MIDI 输入/输出的 Delphi 库?

javascript - Office365 javascript 文件选取器

delphi - 如何通过 idHTTP 获取(即读取)https 响应。通常是 REST 授权

delphi - 如何在 Firemonkey 上将 TImage 显示为 TGrid 的背景?

c# - OneDrive 上传程序不适用于教育或工作场所帐户

powershell - 如何通过使用 Powershell 使用 Get-Credentials 进行身份验证从 OneDrive 下载文件?