验证到谷歌驱动器服务帐户没有json文件在.net



我想验证我的vb.net Winform应用程序访问我的谷歌驱动器帐户。如果我使用json文件,它可以正常工作。但我希望避免在文件系统中放置一个纯json文件,其中包含私钥。特别是当我将另一台机器上安装应用程序。

所以我尝试硬编码凭证参数,到目前为止还没有成功。这是我尝试过的:

Public Function Authenticate() As DriveService

Dim scope As String = DriveService.Scope.Drive
Dim credential As GoogleCredential 
Dim params As New JsonCredentialParameters

With params
.Type = JsonCredentialParameters.ServiceAccountCredentialType
.ProjectId = "myprojid"
.PrivateKeyId = "mykeyid"
.PrivateKey = "-----BEGIN PRIVATE KEY-----n myprivatekey =n-----END PRIVATE KEY-----n"
.ClientEmail = "myappname@myprojid.iam.gserviceaccount.com"
.ClientId = "12345"
End With

credential = GoogleCredential.FromJsonParameters(params).CreateScoped(scope)

Return New DriveService(New BaseClientService.Initializer() With {.HttpClientInitializer = credential, .ApplicationName = "GoogleDriveDownloader"})

End Function

还有类似的方法:

Public Function CreateServiceCredentials() As GoogleCredential
Dim parameters = New JsonCredentialParameters With {
.Type = JsonCredentialParameters.ServiceAccountCredentialType,
.ProjectId = "myprojid",
.PrivateKeyId = "mykeyid",
.PrivateKey = "-----BEGIN PRIVATE KEY-----n myprivatekey =n-----END PRIVATE KEY-----n",
.ClientEmail = "myappname@myprojid.iam.gserviceaccount.com",
.ClientId = "12345"
}
Dim json As String = JsonConvert.SerializeObject(parameters)
Dim stream = New MemoryStream(System.Text.Encoding.UTF8.GetBytes(json))
Return GoogleCredential.FromStream(stream)
End Function

在这两种情况下,我得到错误:ArgumentException: PKCS8数据必须包含在'-----BEGIN私钥-----'和'-----END私钥-----'中。Arg_ParamName_Name

如果我删除后面的n,我得到错误:FormatException:输入不是一个有效的Base-64字符串,因为它包含一个非Base-64字符,两个以上的填充字符,或者填充字符中的非法字符。

我做错了什么?

这是我自己发现的,或者我们称之为变通方法:

我只是把json文件的全部文本放入winforms文本框中,并调用:

credential = GoogleCredential.FromJson(txt_Credentials.Text).CreateScoped(scope)

嗯,它工作;-)

相关内容

  • 没有找到相关文章

最新更新