.NET Google API 访问令牌失败,未指定刷新令牌
.NET Google API access token failing with no refresh token specified
我正在尝试设置一个 class 可以围绕 .NET Google API 以便我可以使用我之前获得的访问令牌来访问用户的 Google 驱动器。截至目前,我只是想让它正常工作,这样我就不需要刷新令牌(稍后会详细介绍)。最终目标是让某人通过我设置的网页进行身份验证,通过直接调用 Google Rest API(我存储在一个数据库)。然后,他们可以在另一个页面上请求 upload/download 文件到他们的云端硬盘,该页面将首先从数据库中获取适当的信息,然后在访问云端硬盘时使用 .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",说明:"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)
感谢您的帮助!
如果您有访问令牌,那么创建 google 凭证的最简单方法是使用 GoogleCredential.FromAccessToken() 方法传入您的访问令牌。
这个 returns 你是一个 GoogleCredential
实例,你可以在构建 DriveService
.
时使用它来设置 HttpClientInitializer
属性
如果您在访问驱动器服务时仍然遇到错误,那么您请求访问令牌的方式可能有误。
我正在尝试设置一个 class 可以围绕 .NET Google API 以便我可以使用我之前获得的访问令牌来访问用户的 Google 驱动器。截至目前,我只是想让它正常工作,这样我就不需要刷新令牌(稍后会详细介绍)。最终目标是让某人通过我设置的网页进行身份验证,通过直接调用 Google Rest API(我存储在一个数据库)。然后,他们可以在另一个页面上请求 upload/download 文件到他们的云端硬盘,该页面将首先从数据库中获取适当的信息,然后在访问云端硬盘时使用 .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",说明:"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)
感谢您的帮助!
如果您有访问令牌,那么创建 google 凭证的最简单方法是使用 GoogleCredential.FromAccessToken() 方法传入您的访问令牌。
这个 returns 你是一个 GoogleCredential
实例,你可以在构建 DriveService
.
HttpClientInitializer
属性
如果您在访问驱动器服务时仍然遇到错误,那么您请求访问令牌的方式可能有误。