0

On my website i would like the user to be able to upload a file they have created to google drive. The file is located on the server.

Currently i am using javascript to authenticate the user and receive an access token. I am then using ajax to post the access token to the my server.

My trouble though is using this access token with the google drive .net sdk v3.

Dim Service = New DriveService()

How do I tell the service to using the accesstoken i have acquired? It seems to require a type of Iauthenticate.

The current flow is working well for facebook and youtube.

Update:

Now getting Error occurred while sending a direct message or getting the response.

I ran fiddler and the response was "error" : "redirect_uri_mismatch" so i changed the AuthState.Callback = New Uri(NativeApplicationClient.OutOfBandCallbackUrl) to reflect my current domain but still get that error. I have made sure the app is setup correct in the app console.

I think its because the redirect uri i am sending in javascript is different to the one I am sending in .net. Javascripts callback url is "postmessage"

I have tried not sending a call back url but it complains it is missing. Have tried setting a callback url of "postmessage" but it complains that it is not a uri.

Any ideas?

4

1 回答 1

0

如果您查看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

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

于 2013-06-24T14:11:57.227 回答