2

我正在尝试将 .Net 的快速入门代码导入 Visual Basic (VB .NET),但出现了一些错误。我是这种编程的新手。将不胜感激一些指针,或者有人指出代码根本错误的东西。

感谢帮助!

我尝试编译控制台应用程序时遇到的错误是:

错误 2 未为“私有共享函数 GetAuthorization(arg As Google.Apis.Authentication.OAuth2.DotNetOpenAuth.NativeApplicationClient)As DotNetOpenAuth.OAuth2.IAuthorizationState”的参数“arg”指定参数。C:\Documents and Settings\Hirak\Local Settings\Application Data\Temporary Projects\Nipod Drive Console\Module1.vb 22 86 Nipod Drive Console

错误 3“BaseClientService”在命名空间“Google.Apis.Services”中不明确。C:\Documents and Settings\Hirak\Local Settings\Application Data\Temporary Projects\Nipod Drive Console\Module1.vb 23 48 Nipod Drive Console

Imports System
Imports System.Diagnostics
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

Namespace GoogleDriveSamples

Class DriveCommandLineSample

    Shared Sub Main(ByVal args As String)

        Dim CLIENT_ID As [String] = "YOUR_CLIENT_ID"
        Dim CLIENT_SECRET As [String] = "YOUR_CLIENT_SECRET"

        '' Register the authenticator and create the service
        Dim provider = New    NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET)
        Dim auth = New OAuth2Authenticator(Of NativeApplicationClient)(provider, GetAuthorization)
        Dim service = New DriveService(New BaseClientService.Initializer() With { _
 .Authenticator = auth _
})

        Dim body As New File()
        body.Title = "My document"
        body.Description = "A test document"
        body.MimeType = "text/plain"

        Dim byteArray As Byte() = System.IO.File.ReadAllBytes("document.txt")
        Dim stream As New System.IO.MemoryStream(byteArray)

        Dim request As FilesResource.InsertMediaUpload = service.Files.Insert(body, stream, "text/plain")
        request.Upload()

        Dim file As File = request.ResponseBody
        Console.WriteLine("File id: " + file.Id)
        Console.WriteLine("Press Enter to end this process.")
        Console.ReadLine()
    End Sub



    Private Shared Function GetAuthorization(ByVal arg As NativeApplicationClient) As IAuthorizationState

        ' Get the auth URL:
        Dim state As IAuthorizationState = New AuthorizationState( New () {DriveService.Scopes.Drive.GetStringValue()})

        state.Callback = New Uri(NativeApplicationClient.OutOfBandCallbackUrl)
        Dim authUri As Uri = arg.RequestUserAuthorization(state)

        ' Request authorization from the user (by opening a browser window):
        Process.Start(authUri.ToString())
        Console.Write("  Authorization Code: ")
        Dim authCode As String = Console.ReadLine()
        Console.WriteLine()

        ' Retrieve the access token by using the authorization code:
        Return arg.ProcessUserAuthorization(authCode, state)

    End Function

End Class


End Namespace
4

2 回答 2

0

我相信这是一个古老的主题,但这可能会在将来对某人有所帮助,请确保您在框架 3.5 中编译

于 2013-06-16T15:13:37.027 回答
0

抱歉,似乎无法编辑或删除我以前的答案,对于将来的任何人,这应该会有所帮助:

Imports System
Imports System.Diagnostics
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 System.Security
Imports Google.Apis.Services


Public Class GoogleDrive
    Public Function UploadFile() As Boolean
        Const CLIENT_ID As String = "xxxxxxxxxxxxx.apps.googleusercontent.com"
        Const CLIENT_SECRET As String = "-yyyyyyyyyyyyyyyyyyyyyyy"

        'Register the authenticator and create the service
        Dim provider As NativeApplicationClient = New NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET)
        Dim getAuth As Func(Of NativeApplicationClient, IAuthorizationState) = AddressOf GetAuthorization
        Dim auth As OAuth2Authenticator(Of NativeApplicationClient) = New OAuth2Authenticator(Of NativeApplicationClient)(provider, getAuth)
        Dim service = New DriveService(New BaseClientService.Initializer() With {.Authenticator = auth})

        Dim body As File = New File()
        body.Title = "My document"
        body.Description = "A test document"
        body.MimeType = "text/plain"

        Dim byteArray As Byte() = System.IO.File.ReadAllBytes("D:\document.txt")
        Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream(byteArray)

        Dim request As FilesResource.InsertMediaUpload = service.Files.Insert(body, stream, "text/plain")
        request.Upload()
        Dim file As File = request.ResponseBody
        MessageBox.Show("File : " & file.Id)
    End Function

    Private Function GetAuthorization(ByVal Client As NativeApplicationClient) As IAuthorizationState

        Dim RetVal As IAuthorizationState
        Dim state As IAuthorizationState = New AuthorizationState(New String() {DriveService.Scopes.Drive.GetStringValue()})

        'Check to see if we have a saved refresh token
        If My.Settings.SavedAuth.ToString <> "" Then

            state.RefreshToken = My.Settings.SavedAuth

            If (Client.RefreshToken(state)) Then
                Return state
            End If
        End If

        'Get the auth URL:
        state.Callback = New Uri(NativeApplicationClient.OutOfBandCallbackUrl)
        Dim authUri As Uri = Client.RequestUserAuthorization(state)

        'Request authorization from the user (by opening a browser window):
        Process.Start(authUri.ToString())

        'wait until user has entered the code
        Dim authCode As String = InputBox("Authorisation code", "Authorisation Code", "")

        'Retrieve the access token by using the authorization code:
        RetVal = Client.ProcessUserAuthorization(authCode, state)

        'store the refresh token
        Call StoreRefreshToken(state.RefreshToken)

        Return RetVal

    End Function

    Private Function LoadRefreshToken() As String

        Return My.Settings.SavedAuth

    End Function

    Private Sub StoreRefreshToken(ByVal Token As String)

        My.Settings("SavedAuth") = Token
        My.Settings.Save()

    End Sub

End Class
于 2013-06-18T20:51:10.543 回答