Google 使用 visual basic .net 驱动 OAuth2 身份验证

Google Drive OAuth2 Authentification with visual basic .net

我正在尝试开发一个 Visual Basic .net 控制台应用程序来从 GoogleDrive 下载和转换文件。到目前为止,它在 OAuth 可用时运行良好。但现在它不再工作了。我在谷歌上搜索了类似的程序,发现了多个代码可以使用以下解决方案。

我将他们的概念应用到我的代码后出现问题:OAuth2 命名空间中缺少 Google Api Class。现在奇怪的是 Google OAuth Api 的文档(对于我使用的 dll 版本是的)说这个 class 必须存在。但它不在那里。我也在 CSharp 中尝试过,但 class 一直丢失。

要下载 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)

编译失败,出现此异常:

The type or namespace name 'ServiceAccountCredential' could not be found >(are you missing a using directive or an assembly reference?)

我正确添加了所有引用,除了 ServiceAccountCredential Class。

没有任何内容被标记为无法解析

参考文献:

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

System | 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 的 Class 说明:https://developers.google.com/api-client-library/dotnet/reference/1.9.1/classGoogle_1_1Apis_1_1Auth_1_1OAuth2_1_1ServiceAccountCredential

另外一个奇怪的事情是class ServiceCredential 应该被ServiceAccountCredential 扩展存在

那么我可以做些什么来解决这个问题,或者可以做些什么来在我的程序中使用 GoogleDrive 而不使用那个特定的 class 来获得有效的 OAuth2 验证?

如有任何帮助,我们将不胜感激。

在花了几个小时找出我做错了什么或者为什么其他人从未听说过这个问题之后,我终于发现我用控制台版本的 nuget 下载的 nuget 包有问题。为了解决问题,您首先需要最新的 Visual Studio 2015 版本,然后使用内置 Nuget 包管理器添加 Google.Apis.Auth。在此之后,上面提到的代码将神奇地发挥作用,因为它应该首先发挥作用。希望我能帮助其他人处理同样的问题。