Vb.net 将Google Drive快速启动代码导入Visual Basic
我正在尝试将.Net的快速启动代码导入Visual Basic(VB.Net),但出现了一些错误。我是这种编程的新手。如果有人能指出代码的根本错误,我会非常感激 谢谢你的帮助 我尝试编译Console应用程序时遇到的错误有: 错误2未将“私有共享函数GetAuthorization(arg为Google.api.Authentication.OAuth2.DotNetOpenAuth.NativeApplicationClient)的参数“arg”指定为DotNetOpenAuth.OAuth2.IAuthorizationState”。C:\Documents and Settings\Hirak\Local Settings\Application Data\Temporary Projects\Nipod Drive Console\Module1.vb 22 86 Nipod Drive Console 错误3“BaseClientService”在命名空间“Google.api.Services”中不明确。C:\Documents and Settings\Hirak\Local Settings\Application Data\Temporary Projects\Nipod Drive Console\Module1.vb 23 48 Nipod Drive ConsoleVb.net 将Google Drive快速启动代码导入Visual Basic,vb.net,google-drive-api,Vb.net,Google Drive Api,我正在尝试将.Net的快速启动代码导入Visual Basic(VB.Net),但出现了一些错误。我是这种编程的新手。如果有人能指出代码的根本错误,我会非常感激 谢谢你的帮助 我尝试编译Console应用程序时遇到的错误有: 错误2未将“私有共享函数GetAuthorization(arg为Google.api.Authentication.OAuth2.DotNetOpenAuth.NativeApplicationClient)的参数“arg”指定为DotNetOpenAuth.OAuth2
我相信这是一个古老的主题,但这可能对将来的人有所帮助,请确保您在framework 3.5中编译,抱歉,似乎无法编辑或删除我以前的答案,对于将来的任何人,这应该会有所帮助:
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
导入系统
导入系统。诊断
导入DotNetOpenAuth.OAuth2
导入Google.api.Authentication.OAuth2
导入Google.api.Authentication.OAuth2.DotNetOpenAuth
导入Google.api.Drive.v2
导入Google.api.Drive.v2.Data
导入Google.api.Util
导入系统。安全
导入Google.api.Services
公共级谷歌硬盘
公共函数UploadFile()为布尔值
Const CLIENT_ID为String=“xxxxxxxxxxxx.apps.googleusercontent.com”
Const CLIENT_SECRET As String=“-yyyyyyyyyyyyyyyyyyyyy”
'注册验证器并创建服务
Dim provider As NativeApplicationClient=新的NativeApplicationClient(GoogleAuthenticationServer.Description,CLIENT\u ID,CLIENT\u SECRET)
Dim getAuth As Func(对于NativeApplicationClient,IAAuthorizationState)=GetAuthorization的地址
Dim auth As OAuth2Authenticator(NativeApplicationClient的)=新OAuth2Authenticator(NativeApplicationClient的)(提供程序,getAuth)
Dim服务=新的DriveService(新的BaseClientService.Initializer(),带有{.Authenticator=auth})
Dim body As File=新文件()
body.Title=“我的文档”
body.Description=“测试文档”
body.MimeType=“text/plain”
Dim byteArray As Byte()=System.IO.File.ReadAllBytes(“D:\document.txt”)
作为System.IO.MemoryStream的Dim流=新System.IO.MemoryStream(byteArray)
Dim请求为fileResource.InsertMediaUpload=service.Files.Insert(正文、流、“文本/普通”)
请求上传
Dim file As file=request.ResponseBody
MessageBox.Show(“文件:”&File.Id)
端函数
私有函数GetAuthorization(ByVal客户端作为NativeApplicationClient)作为IAAuthorizationState
Dim RetVal作为IAuthorizationState
作为IAAuthorizationState的Dim状态=新授权状态(新字符串(){DriveService.Scopes.Drive.GetStringValue()})
'检查是否保存了刷新令牌
如果My.Settings.SavedAuth.ToString“”则
state.RefreshToken=My.Settings.SavedAuth
如果是(Client.RefreshToken(state)),则
返回状态
如果结束
如果结束
'获取身份验证URL:
state.Callback=新Uri(NativeApplicationClient.OutOfBandCallbackUrl)
Dim authUri作为Uri=Client.RequestUserAuthorization(状态)
'请求用户授权(通过打开浏览器窗口):
Process.Start(authUri.ToString())
'等待用户输入代码
Dim authCode As String=InputBox(“授权代码”、“授权代码”、“”)
'使用授权代码检索访问令牌:
RetVal=Client.ProcessUserAuthorization(authCode,state)
'存储刷新令牌
调用StoreRefreshToken(state.RefreshToken)
返回返回
端函数
私有函数LoadRefreshToken()作为字符串
返回My.Settings.SavedAuth
端函数
私有子存储刷新令牌(ByVal令牌作为字符串)
My.Settings(“SavedAuth”)=令牌
My.Settings.Save()
端接头
末级
我不确定与BaseClientService
相关的错误,但对于arg
(错误2)周围的错误,我认为这是因为:Dim auth=New OAuth2Authenticator(NativeApplicationClient的)(provider,GetAuthorization)
应该是这样的:Dim auth=New OAuth2Authenticator(针对本地应用程序客户)(提供程序、GetAuthorization的地址)
instead@nkvu谢谢!它需要'AddressOf',而该更改确实解决了该错误。基本客户端是由于Silverlight.dll库也被添加到了项目中。它不需要在那里。我去掉了它,这个错误就消失了。将状态定义更改为IAAuthorizationState=New Authorization ationState(新的DotNetOpenAuth.OAuth2.AuthorizationState,带有{.AccessToken=DriveService.Scopes.Drive.GetStringValue()})消除了编译错误。但是,在request.upload()
处出现运行时错误,因为无法将类型为“DotNetOpenAuth.OAuth2.AuthorizationState”的对象强制转换为类型“System.Collections.Generic.IEnumerable`1”[System.String]'。
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