2

Visual Basic (VB .NET) にインポートされた .Net のクイックスタート コードを取得しようとしていますが、エラーが発生しました。私はこの種のプログラミングの初心者です。いくつかのポインタ、またはコードの根本的な間違いを指摘してくれる人に感謝します。

助けてくれてありがとう!

コンソール アプリをコンパイルしようとすると、次のエラーが表示されます。

エラー 2 'Private Shared Function 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

申し訳ありませんが、以前の回答を編集または削除することはできません。将来的には、これが役立つはずです。

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 に答える