我正在尝试建立一个可以包装.NET Google API的类,以便可以使用以前获得的访问令牌访问用户的Google云端硬盘。截至目前,我只是试图使其工作而不再需要刷新令牌(稍后会详细介绍)。最终目标是让某人浏览我设置的网页,以通过直接调用Google Rest API(我存储在数据库中)来获取访问令牌和刷新令牌的身份。然后,他们可以请求将文件上传/下载到其他页面上的云端硬盘中,该页面将首先从数据库中获取适当的信息,然后在访问云端硬盘时使用.NET Google API库。
但是,当我尝试访问其云端硬盘时,出现以下错误:
访问令牌已过期,无法刷新。错误:刷新错误,刷新错误,刷新错误
我知道访问令牌是有效的,因为我在测试过程中仅几秒钟前获得了它。这是我用于设置驱动器服务的代码:
' 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”,说明:“未经授权”,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)
感谢您的帮助!
答案 0 :(得分:0)
如果您有访问令牌,那么创建Google凭据的最简单方法是使用GoogleCredential.FromAccessToken()方法,并传入访问令牌。
这将返回一个GoogleCredential
实例,您可以在构建HttpClientInitializer
时使用它来设置DriveService
属性。
如果访问驱动器服务时仍然出现错误,则可能是您请求访问令牌的方式有误。