访问令牌过期后如何继续上传? [英] How to continue an upload after Access Token expire?

查看:416
本文介绍了访问令牌过期后如何继续上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.net中的桌面应用程序将文件上传到Google云端硬盘。要授权的应用程序使用服务帐户。问题在于大文件(+ 300MB)。上传1小时后,应用程序会显示错误消息:


无效凭证


问题是Access令牌过期,我不知道如何刷新凭证。



创建DriveService为:

$ p $ 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 =新的OAuth2Authenticator(Of AssertionFlowClient)(provider,AddressOf AssertionFlowClient.GetState)

auth.LoadAccessToken()
返回认证
End Function

Public Function getService()As DriveService
返回新的DriveService(getAuthenticator())
End Function

并且上传的函数是:

 公共函数上传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(PAT hFile)
Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream(byteArray)

fi.Title = fileName
Dim请求As FilesResource.InsertMediaUpload = service。 Files.Insert(fi,stream,* / *)
request.ChunkSize = 1048576

AddHandler request.ProgressChanged,New Action(IUploadProgress)(AddressOf Request_ProgressChanged)

request.Upload()

Dim fileUpload As File = request.ResponseBody

Return fileUpload
End Function

我读过文档,但没有找到这种情况的例子。



更新



我将认证应用程序更改为使用以前保存的刷新令牌的常规Google帐户,但结果相同。

此问题与问题相关 264 ,我的问题是:是否有可能使上传时间超过1小时? 解决方案

除了云端硬盘OAuth范围外,您还需要请求离线access_type。当您请求时,auth响应将包含一个刷新令牌,当旧的令牌到期时,您可以使用该令牌来获取新的访问令牌。



图书馆应该注意其中大部分都是透明的,您可能只需在您的身份验证请求中传递离线作用域即可。 更多信息: https://developers.google.com/accounts/docs/OAuth2WebServer#offline


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?

解决方案

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

这篇关于访问令牌过期后如何继续上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆