当前位置: 首页 > 编程语言 > C#

Google云端硬盘,上传文件时刷新授权令牌共享

时间:2023-04-10 21:00:21 C#

GoogleDrive。上传文件时刷新授权令牌上传大文件到GoogleDrive时刷新授权令牌的方法是什么?示例:当前授权令牌在“06:00AM”过期。文件上传开始于“05:15AM”。因此,在“06:00AM”,应用程序收到异常,因为授权令牌无效。我尝试使用以下源代码来解决这个问题,但它不起作用。//////上传指定路径的文件。//////起始文件夹。///目标文件的路径。///要上传的本地文件。///上传的文件。私有文件GdUploadFile(FilestartFolder,stringpath,FileInfolocalFile){if(startFolder==null){thrownewArgumentNullException("startFolder");}if(localFile==null){thrownewArgumentNullException("localFile");}if(!localFile.Exists){thrownewFileNotFoundException("找不到文件",localFile.FullName);}varconfig=GetConfiguration();if(config.TraceLog){Destination.Logger.LogDebug(string.Format("{0}tUpload文件"{1}"到"{2}"",Destination.Name,localFile.FullName,path));}stringpathToFile=string.IsNullOrEmpty(path)?localFile.Name:路径;字符串remotePath=ExtractFilePath(pathToFile);varfileFolder=GdCreateFolderByPath(startFolder,remotePath);varfileName=ExtractFileName(pathToFile);DriveService服务=GetDriveService();varbody=新文件{Title=fileName,Description="MyFile",MimeType=BackupFileMimeType,Kind=DriveFileKindType,OriginalFilename=fileName,FileExtension=localFile.Extension,Parents=newList{newParentReference{ETag=fileFolder.ETag,Id=fileFolder.Id,Kind=fileFolder.Kind}}};FilesResource.InsertMediaUpload请求;varsource=newMediaFileSource(localFile.FullName,BackupFileMimeType);使用(varfileStream=source.GetDataStream()){if(config.TraceLog){Destination.Logger.LogDebug(string.Format("{0}tUploading"{1}"...",Destination.Name,localFile.全名));}request=service.Files.Insert(body,fileStream,body.MimeType);如果(config.TraceLog){intpostedPercent=0;request.ProgressChanged+=p=>{varcurrentPercent=(int)(p.BytesSent/(double)source.ContentLength*100);if(currentPercent!=postedPercent){stringmsg=string.Format("{0}tPosted{1}%({2}bytes)",Destination.Name,currentPercent,p.BytesSent);目的地nation.Logger.LogDebug(味精);postedPercent=currentPercent;}};}varconnection=Destination.ConnectionasGoogleDriveDestinationConnection;Debug.Assert(connection!=null,"connection!=null");request.ProgressChanged+=p=>{boolrefreshAuth=connection.ForceRefreshAuthorization();varauth=request.AuthenticatorasGoogle.Apis.Authentication.OAuth2.OAuth2Authenticator;if(auth!=null&&auth.State!=null&&refreshAuth){varstate=connection.AuthorizationState;auth.State.AccessToken=state.AccessToken;auth.State.AccessTokenExpirationUtc=state.AccessTokenExpirationUtc;auth.State.AccessTokenIssueDateUtc=state.AccessTokenIssueDateUtc;auth.State.Callback=state.Callback;auth.State.RefreshToken=state.RefreshToken;auth.State.SaveChanges();if(config.TraceLog){Destination.Logger.LogDebug("上传请求的授权状态已更新");}}};request.ChunkSize=ChunkSize;请求.上传();如果(config.TraceLog){Destination.Logger.LogDebug(string.Format("{0}t"{1}"上传",Destination.Name,localFile.FullName));}}返回请求.ResponseBody;}考虑进行可恢复上传(https://developers.google.com/drive/manage-uploads#resumable)在需要时刷新令牌并从中断的地方恢复上传。对于无效授权令牌的问题,我找不到令人满意的解决方案。因此,我创建了自己的开源轻量级库,用于处理GoogleDrive上的文件。该库直接调用所有REST函数,并完全控制上传或下载过程。它通过每小时刷新一次授权令牌来解决问题。这个库目前在我公司的SqlServer备份产品上千次安装中使用,非常稳定,成功解决了这个问题。您可以从这里获取源代码和示例:https://github.com/AlexeyVlg/Pranas.Net.Clouds以上是C#学习教程:GoogleDrive。上传文件时刷新授权令牌共享的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击权利联系管理会员删除。如需转载请注明出处: