2016-04-27 77 views
1

我想在Delphi中使用RestAPI将文件上传到一个驱动器。但无法将文件上传到一个驱动器。验证一个驱动器成功,但在上传文件时出错。以下是我收到的错误消息。当我试图上传文件到使用Delphi的一个驱动器时得到错误的请求错误

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)获得例外。任何机构可以建议如何解决这个问题?

+1

你应该改变你的应用程序的ClientSecret值,当它被外人知道时,这个值的安全性会受到影响。 –

回答

2

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

要执行上传,您需要构建如下的url,然后将文件内容放入该位置。有更多上传选项可能更适合您的应用程序的情况,请了解它们here

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

的OneDrive API在https://dev.onedrive.com/记录周边与OneDrive元数据,以及上传下载互动的场景全庄园。

+0

我们在从一个驱动器检索访问令牌时收到异常。我们评论了“//在此获得例外”评论。你能否提出建议是什么问题。 – sanjeev

相关问题