我正在尝试使用Delphi中的RestAPI将文件上传到一个驱动器。但无法将文件上传到一个云端硬盘。对一个驱动器的身份验证成功,但在上载文件时出错。以下是我收到的错误消息。
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)。任何机构都可以建议如何解决这个问题吗?
答案 0 :(得分:2)
要与OneDrive上的文件进行互动,您应该使用https://api.onedrive.com
基本网址。
要执行上传,您需要构建如下所示的网址,然后将文件内容放入该位置。有更多上传选项可以更好地适应您的应用程序的情况,了解它们here。
https://api.onedrive.com/v1.0/drive/root:/{parent-path}/{filename}:/content
OneDrive API记录在https://dev.onedrive.com/,用于与OneDrive中的元数据交互以及上传和下载的各种场景。