我正在尝试开发Visual Basic .net控制台应用程序,以便从GoogleDrive下载和转换文件。到目前为止,它在OAuth可用时完美运行。但现在它不再起作用了。我搜索了类似的程序,发现多个代码使用以下解决方案。
我将自己的概念应用到我的代码后出现问题:OAuth2命名空间中缺少Google Api类。现在奇怪的是,Google OAuth Api的文档(我使用的dll版本是的)说这个类必须存在。但它不在那里。我也在CSharp中尝试过,但课程一直缺失。
要下载Google Api,我使用了nuget。
我的代码:
Imports Google.Apis
Imports Google.Apis.Auth
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Services
Imports Google.GData.Client
Imports Google.GData.Extensions
Imports Google.GData.Spreadsheets
Imports System
Imports System.Linq
Imports System.Text
Imports System.Web
Imports System.Net
Imports System.IO
Imports System.Threading
Imports System.Threading.Tasks
Sub DownloadFile(strUsr As String, strPwd As String, strGoogleFileId As String, strTargetFile As String, strFormatId As String, boolDebug As Boolean)
Try
Dim strServiceUrl As String = "http://spreadsheets.google.com/feeds"
Dim strRequestUrl As String = strServiceUrl + "/download/spreadsheets/Export?key=" + strGoogleFileId + "&exportFormat=" + strFormatId
Dim query As New SpreadsheetQuery(strRequestUrl)
Dim ssservice As New SpreadsheetsService("GoogleDriveDownloader_AppDesigners_de")
Login(strUsr, strPwd, ssservice, LoginMode.OAuth2)
Dim Stream As Stream = ssservice.Query(New Uri(strRequestUrl))
Dim fStream As New FileStream(strTargetFile, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)
Stream.CopyTo(fStream)
fStream.Flush()
fStream.Close()
Console.WriteLine("Downloaded GoogleSpreadSheet with Id '" + strGoogleFileId + "' to File '" + strTargetFile + "'")
Catch ex As Exception
Console.WriteLine("Error: " + ex.Message)
If boolDebug Then
Console.WriteLine("Source: " + ex.Source)
Console.WriteLine("Call Stack: " + ex.StackTrace)
End If
End Try
End Sub
Enum LoginMode
OAuth1
OAuth2
End Enum
Sub Login(strUsr As String, strPwd As String, ssservice As SpreadsheetsService, mode As LoginMode)
If mode = LoginMode.OAuth1 Then
LoginOAuth1(strUsr, strPwd, ssservice)
ElseIf mode = LoginMode.OAuth2 Then
LoginOAuth2(strUsr, strPwd, ssservice)
End If
End Sub
Sub LoginOAuth2(strUsr As String, strPwd As String, ssservice As SpreadsheetsService)
Dim strServiceUrl As String = "http://spreadsheets.google.com/feeds"
Dim strServiceEmail As String = "<mygoogleacc>"
Dim certificate As New X509Certificate2("d:\GoogleDriveConverter\cert.p12",
"<certpwd>",
X509KeyStorageFlags.Exportable)
Dim credentialInitializer As New ServiceAccountCredential(New ServiceAccountCredential.Initializer(strServiceEmail))
credentialInitializer.User = strUsr
credentialInitializer.Scopes = New String() {strServiceUrl}
Dim credential As New ServiceAccountCredential(credentialInitializer.FromCertificate(certificate))
If (Not credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result) Then
Throw New InvalidOperationException("Access token request failed.")
End If
Dim requestFactory = New GDataRequestFactory("GoogleDriveDownloader_AppDesigners_de")
requestFactory.CustomHeaders.Add("Authorization: Bearer " + credential.Token.AccessToken)
ssservice.RequestFactory = requestFactory
End Sub
Sub LoginOAuth1(strUsr As String, strPwd As String, ssservice As SpreadsheetsService)
ssservice.setUserCredentials(strUsr, strPwd)
End Sub
(OAuth2基于http://www.afterlogic.com/mailbee-net/docs/OAuth2GoogleServiceAccounts.html的身份验证)
编译因以下异常而失败:
无法找到类型或命名空间名称“ServiceAccountCredential”&gt;(您是否缺少using指令或程序集引用?)
我正确添加了所有引用,除了ServiceAccountCredential类外,没有任何内容被标记为不可解析。
参考文献:
Google.Apis | 1.9.0.26011 | d:\ GoogleDriveConverter \ Google.Apis.1.9.0 \ lib中\ net40 \ Google.Apis.dll
Google.Apis.Auth | 1.9.0.26011 | d:\ GoogleDriveConverter \ Google.Apis.Auth.1.9.0 \ lib中\ net40 \ Google.Apis.Auth.dll
Google.GData.Client | 2.2.0.0 | d:\ GoogleDriveConverter \的Redist \ Google.GData.Client.dll
Google.GData.Extensions | 2.2.0.0 | d:\ GoogleDriveConverter \的Redist \ Google.GData.Extensions.dll
Google.GData.Spreadsheets | 2.2.0.0 | d:\ GoogleDriveConverter \的Redist \ Google.GData.Spreadsheets.dll
系统| 4.0.0.0 | C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.0 \ System.dll
System.Core | 4.0.0.0 | C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.0 \ System.Core.dll
System.Data | 4.0.0.0 | C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.0 \ System.Data.dll
System.Data.DataSetExtensions | 4.0.0.0 | C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.0 \ System.Data.DataSetExtensions.dll
System.Deployment | 4.0.0.0 | C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.0 \ System.Deployment.dll
System.Xml | 4.0.0.0 | C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.0 \ System.Xml.dll
System.Xml.Linq | 4.0.0.0 | C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.0 \ System.Xml.Linq.dll
可以在此处找到ServiceAccountCredential的类描述:https://developers.google.com/api-client-library/dotnet/reference/1.9.1/classGoogle_1_1Apis_1_1Auth_1_1OAuth2_1_1ServiceAccountCredential
另一个奇怪的事情是应该由ServiceAccountCredential扩展的ServiceCredential类存在。
那么我该怎样做才能清除这个问题,或者我可以做些什么来在我的程序中使用GoogleDrive进行有效的OAuth2验证而不使用那个特定的类?
任何帮助都将不胜感激。
答案 0 :(得分:0)
花了好几个小时才发现我做错了什么或为什么其他人都没有听说过这个问题我终于发现我用nuget的控制台版本下载的nuget包有问题。为了解决问题,您首先需要最新的Visual Studio 2015版本,然后使用Buildin Nuget Package Manager添加Google.Apis.Auth。在此之后,上述代码将首先神奇地起作用。希望我能帮助别人处理同样的问题。