【发布时间】:2018-10-19 14:21:35
【问题描述】:
我正在尝试设置一个可以包装 .NET Google API 的类,以便我可以使用之前获得的访问令牌来访问用户的 Google Drive。截至目前,我只是想让它工作,这样我就不需要刷新令牌(稍后会详细介绍)。最终目标是让某人通过我设置的网页进行身份验证,通过直接调用 Google Rest API(我存储在数据库中)获得访问令牌和刷新令牌。然后,他们可以请求在不同页面上将文件上传/下载到他们的 Drive,这将首先从数据库中获取适当的信息,然后在访问 Drive 时使用 .NET Google API 库。
但是,当我尝试访问他们的云端硬盘时,出现以下错误:
访问令牌已过期,无法刷新。错误:刷新错误、刷新错误、刷新错误
我知道访问令牌是有效的,因为我在测试期间仅提前几秒钟获得了它。这是我设置 Drive Service 的代码:
' NOTE: Code altered for brevity
Public Sub Initialize(accessToken As String)
' Set up the client secret information based on the default constants
Dim clientSecrets As New ClientSecrets()
clientSecrets.ClientId = DEFAULT_CLIENT_ID
clientSecrets.ClientSecret = DEFAULT_CLIENT_SECRET
' Set up a token based on the token data we got
' NOTE: Is it OK to leave some strings as NULL?
Dim token As New Responses.TokenResponse()
token.AccessToken = accessToken
token.RefreshToken = ""
token.TokenType = "Bearer"
token.IssuedUtc = DateTime.Now
token.ExpiresInSeconds = 3600
token.Scope = "drive"
token.IdToken = ""
' Set up a flow for the user credential
Dim init As New GoogleAuthorizationCodeFlow.Initializer()
init.ClientSecrets = clientSecrets
init.Scopes = New String() {DriveService.Scope.Drive}
init.Clock = Google.Apis.Util.SystemClock.Default
' Set up everything else and initialize the service
Dim baseInit As New BaseClientService.Initializer()
baseInit.HttpClientInitializer = New UserCredential(New GoogleAuthorizationCodeFlow(init), "user", token)
baseInit.ApplicationName = APP_NAME
_service = New DriveService(baseInit)
End Sub
不久之后,我使用以下代码请求一个文件夹,以便检查它是否存在。
Private Function GetDriveFolder(folderPath As String, ByRef folderIds As String(), Optional createMissingFolders As Boolean = False, Optional parentFolderId As String = "root") As Data.File
Dim creatingFolderPath As Boolean = False
Dim currentFolder As Data.File = Nothing
Dim folderPathSplit As String() = folderPath.Replace("/", "\").Trim("\").Split("\")
Dim folderIdList As New List(Of String)
folderIds = {}
' Loop through each folder in the path and seek each out until we reach the end
For x As Integer = 0 To folderPathSplit.Length - 1
Dim result As FileList = Nothing
If Not creatingFolderPath Then
' Build a list request which we will use to seek out the next folder
Dim request As FilesResource.ListRequest = _service.Files.List()
request.Q = "mimeType='application/vnd.google-apps.folder' and name='" & folderPathSplit(x) & "'"
If currentFolder Is Nothing Then
request.Q &= " and '" & EscapeDriveValue(parentFolderId) & "' in parents"
Else
request.Q &= " and '" & EscapeDriveValue(currentFolder.Id) & "' in parents"
End If
request.Spaces = "drive"
request.Fields = "files(id, name)"
' Execute the search, we should only get a single item back
' NOTE: Error thrown on this request
result = request.Execute()
End If
' So on.....
所以,我暂时只是想让它只使用访问令牌,因为如果它最终被刷新,我需要知道这样我才能更新我的数据库。但是,如果我确实包含了刷新令牌,我会收到以下错误:
错误:“unauthorized_client”,描述:“Unauthorized”,Uri:“”
我猜这与我通过开发控制台配置应用程序的方式有关,但如果我通过 Google API 库通过启动浏览器来获取我的凭据进行身份验证,一切正常。所以,我真的不确定从这里去哪里,因为我没有发现任何人有类似的问题,而且指南没有涵盖指定你自己的访问令牌。
另外,作为一个快速说明,这是我在让用户进行身份验证时使用的 URL:
String.Format("https://accounts.google.com/o/oauth2/v2/auth?client_id={0}&state={1}&redirect_uri={2}&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&access_type=offline&include_granted_scopes=true&prompt=select_account%20consent&response_type=code", GOOGLEAPI_CLIENTID, validateState, redirectUri)
感谢您的帮助!
【问题讨论】:
标签: vb.net google-api google-drive-api google-api-dotnet-client