How to continue an upload after Access Token expire?

百般思念 提交于 2019-12-24 21:18:01

问题


I have a desktop application in .net to upload files to Google Drive. The app to authorize uses a Service Account. The issue is with large files (+ 300MB). After 1 hour of upload the application raises an error with the message :

Invalid Credentials

The problem is that Access Token expired and I don't know how to refresh the credentials.

The functions to create the DriveService are:

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

And the function to upload is:

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

I read the documentation, but I don't find an example for this case.

Update

I changed the authentication application to use a regular Google account with a refresh token saved previously, but the the result is the same.
This problem is related with the issue 264, and my question is: Is possible to make an upload more than 1 hour?


回答1:


In addition to the Drive OAuth scope, you also need to request the "offline" access_type. When you request this, the auth response will include a refresh token, which you can use to get a new access token when the old one expires.

The library should take care of most of this transparently, you may just need to pass the "offline" scope in your auth request.

More info: https://developers.google.com/accounts/docs/OAuth2WebServer#offline




回答2:


I don't recognize the library you're using, but if it gives access to all the OAuth2 bells & whistles, you ought to be able to get a refresh token, and with that, get a new access token either at or before the time it expires.



来源:https://stackoverflow.com/questions/14628024/how-to-continue-an-upload-after-access-token-expire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!