我在 .net 中有一个桌面应用程序,可以将文件上传到 Google Drive。要授权的应用程序使用服务帐户。问题在于大文件(+ 300MB)。上传 1 小时后,应用程序会出现错误消息:
无效证件
问题是访问令牌已过期,我不知道如何刷新凭据。
创建 DriveService 的函数有:
Public Function getAuthenticator() As OAuth2Authenticator(Of AssertionFlowClient)
Dim SERVICE_ACCOUNT_EMAIL As String = <email>
Dim SERVICE_ACCOUNT_PKCS12_FILE_PATH As String = <pathFile>
Dim certificate As X509Certificate2 = New X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, <secret>, X509KeyStorageFlags.Exportable)
Dim provider = New AssertionFlowClient(GoogleAuthenticationServer.Description, certificate) With {.ServiceAccountId = SERVICE_ACCOUNT_EMAIL, .Scope = DriveService.Scopes.Drive.GetStringValue}
Dim auth = New OAuth2Authenticator(Of AssertionFlowClient)(provider, AddressOf AssertionFlowClient.GetState)
auth.LoadAccessToken()
Return auth
End Function
Public Function getService() As DriveService
Return New DriveService(getAuthenticator())
End Function
上传的功能是:
Public Function upload(pathFile As String, fileName As String) As File
Dim service = getService()
Dim fi As File = New Data.File()
Dim byteArray As Byte() = System.IO.File.ReadAllBytes(pathFile)
Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream(byteArray)
fi.Title = fileName
Dim request As FilesResource.InsertMediaUpload = service.Files.Insert(fi, stream, "*/*")
request.ChunkSize = 1048576
AddHandler request.ProgressChanged, New Action(Of IUploadProgress)(AddressOf Request_ProgressChanged)
request.Upload()
Dim fileUpload As File = request.ResponseBody
Return fileUpload
End Function
我阅读了文档,但没有找到这种情况的示例。
更新
我将身份验证应用程序更改为使用带有先前保存的刷新令牌的常规 Google 帐户,但结果是相同的。
这个问题与问题264有关,我的问题是:是否可以上传超过 1 小时?