使用现有的访问令牌在.net中请求谷歌驱动器

Using existing access token for google drive request in .net

本文关键字:net 请求 谷歌 驱动器 访问令牌      更新时间:2023-09-26

在我的网站上,我希望用户能够将他们创建的文件上传到谷歌驱动器。该文件位于服务器上。

目前,我正在使用javascript对用户进行身份验证并接收访问令牌。然后我使用ajax将访问令牌发布到我的服务器。

不过,我的问题是在googledrive.netsdkv3中使用这个访问令牌。

Dim Service = New DriveService()

如何告知服务使用我获得的accesstoken?它似乎需要一种身份验证。

目前的流量在facebook和youtube上运行良好。

更新:

现在发送直接消息或获取响应时发生获取错误。

我运行了fiddler,得到的响应是"error":"redirect_uri_mismatch",所以我更改了AuthState.Callback=New uri(NativeApplicationClient.OutOfBandCallbackUrl)以反映我当前的域,但仍然出现了该错误。我已确保应用程序控制台中的应用程序设置正确。

我认为这是因为我在javascript中发送的重定向uri与我在.net中发送的不同。javascript回调url是"postmessage"

我试过不发送回拨网址,但它抱怨它不见了。尝试设置一个回调url"postmessage",但它抱怨它不是uri。

有什么想法吗?

如果你看一下C#快速入门,你可以看到控制台应用程序创建了你使用JS发出的请求,并要求用户写回AuthToken。

这里有一个类似于我用来获取DriveService对象的VB代码

    Imports System
    Imports System.Diagnostics
    Imports System.Configuration
    Imports DotNetOpenAuth.OAuth2
    Imports Google.Apis.Authentication.OAuth2
    Imports Google.Apis.Authentication.OAuth2.DotNetOpenAuth
    Imports Google.Apis.Drive.v2
    Imports Google.Apis.Drive.v2.Data
    Imports Google.Apis.Util
    Imports Google.Apis.Services
 Private Shared CLIENT_ID As String
    Private Shared CLIENT_SECRET As String
    Private Shared AUTH_CODE As String
    Private Shared FOLDER_NAME As String
    Private Shared FolderId As String
    Private Shared oProvider As NativeApplicationClient
    Private Shared oAuth As OAuth2Authenticator(Of NativeApplicationClient)
    Private Shared oDriveService As DriveService
        Private Shared Sub Initialize(nAuthCode as string)
            If CLIENT_ID Is Nothing Or CLIENT_SECRET Is Nothing Or AUTH_CODE Is Nothing Then
                'MSO - 20130423 - Read from the config the CLIENT_ID and the Secret if they are empty
                CLIENT_ID = ConfigurationManager.AppSettings("CLIENT_ID")
                CLIENT_SECRET = ConfigurationManager.AppSettings("CLIENT_SECRET")
                AUTH_CODE = nAuthCode 
                'Auth Provider
                oProvider = New NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET)
                'Create OAuth2 autentication using the previously generated Auth Key
                oAuth = New OAuth2Authenticator(Of NativeApplicationClient)(oProvider, AddressOf GetAuthorization)
                'Initialize the DriveService Object
                Dim oBasicInit As New BaseClientService.Initializer()
                oBasicInit.Authenticator = oAuth
                oDriveService = New DriveService(oBasicInit)
            End If
        End Sub
        Private Shared Function GetAuthorization(arg As NativeApplicationClient) As IAuthorizationState

            'Auth Scope (in this case Google Drive Read Only)
            Dim AuthState As IAuthorizationState = New AuthorizationState({DriveService.Scopes.DriveReadonly.GetStringValue()})
            AuthState.Callback = New Uri(NativeApplicationClient.OutOfBandCallbackUrl)
AuthState = arg.ProcessUserAuthorization(AUTH_CODE, AuthState)    
            Return AuthState
        End Function

代码应该可以工作,让我知道是否有任何问题