【问题标题】:Google Drive OAuth2 Authentification with visual basic .net使用 Visual Basic .net 进行 Google Drive OAuth2 身份验证
【发布时间】:2015-10-04 18:33:52
【问题描述】:

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

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

要下载我使用 nuget 的 Google Api。

我的代码:

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

(基于http://www.afterlogic.com/mailbee-net/docs/OAuth2GoogleServiceAccounts.html的OAuth2认证)

编译失败,出现以下异常:

找不到类型或命名空间名称“ServiceAccountCredential”>(您是否缺少 using 指令或程序集引用?)

我正确添加了所有引用,除了 ServiceAccountCredential 类之外,没有任何内容被标记为不可解析。

参考资料:

Google.API | 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

系统.数据 | 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

系统.部署 | 4.0.0.0 | C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\System.Deployment.dll

系统.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 验证而不使用该特定类?

任何帮助将不胜感激。

【问题讨论】:

    标签: vb.net oauth google-api google-drive-api google-oauth


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-04-29
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 2012-11-20
      • 2021-01-16
      • 2017-08-04
      • 1970-01-01
      相关资源
      最近更新 更多