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

时间:2013-06-24 01:20:02

标签: javascript .net google-drive-api

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

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

我的麻烦是将此访问令牌与google驱动器.net sdk v3一起使用。

Dim Service = New DriveService()

如何告诉服务使用我已获得的accessstoken?它似乎需要一种Iauthenticate。

目前的流量适用于facebook和youtube。

更新

现在发送直接消息或收到回复时出现错误。

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

我认为是因为我使用javascript发送的重定向uri与我发送的.net不同。 Javascripts回调网址是“postmessage”

我已经尝试过不回电话了,但是它抱怨它丢失了。 尝试设置“postmessage”的回调网址,但它抱怨它不是uri。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果您查看C# Quick start,您可以看到控制台应用程序创建您使用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

如果有任何问题,代码应该可以告诉我