【发布时间】:2022-01-01 19:06:43
【问题描述】:
我被这个问题困住了。我正在使用带有 P12 文件的 C# 中的 Google Drive。它在我的本地机器和服务器上运行良好。我需要将它部署在客户端新服务器上,当我尝试获取返回无效算法的文件时遇到此问题。相同的设置在我的本地机器和其他服务器上工作正常。这似乎是机器特定的问题。应用程序在 Windows 7 Professional 64 位操作系统上运行。 服务已成功创建证书已读取但以下代码返回错误这是我返回错误的代码
FilesResource.ListRequest list = service.Files.List();
list.PageSize = 100;
files = list.Execute().Files; /// This line has error
可能是防火墙问题。防火墙应该打开什么?
这是我的授权码:
public override DriveService AuthenticateServiceAccount(DriveRequest request)
{
if (request == null)
return null;
string errorMessage = "Exception: invalid Drive Request";
if (string.IsNullOrEmpty(request.EmailAccount) || string.IsNullOrEmpty(request.KeyFilePath))
return null;
errorMessage = "Exception: Keyfile not found on the path-" + request.KeyFilePath;
// check the file exists
if (!System.IO.File.Exists(request.KeyFilePath))
{
Commons.Utilities.WriteLog(errorMessage);
return null;
}
//---- This scope is used For G-Suite Account
string[] scopes = new string[] { DriveService.Scope.Drive };
errorMessage = "Exception: issue with certificate";
var certificate = new X509Certificate2(request.KeyFilePath, "notasecret", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);
try
{
ServiceAccountCredential credential = null;
if (request.UserAccount != "" && request.UserAccount != null)
{
credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(request.EmailAccount)
{
Scopes = scopes,
User = request.UserAccount
}.FromCertificate(certificate));
}
else {
credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(request.EmailAccount)
{
Scopes = scopes
}.FromCertificate(certificate));
}
errorMessage = "Exception: while creating google service";
if (service == null) {
// Create the service.
service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Simplicity Cloud Project" // User defined any name can be given
}) ;
}
return service;
}
catch (Exception ex)
{
//Console.WriteLine(ex.InnerException);
Commons.Utilities.WriteLog(errorMessage);
return null;
}
}
这是生成的日志:
读取证书 2021-11-22 15:25:58,416 |SimplicityOnlineWebApi.Commons.Utilities|信息| - 证书已读:[主题] CN=102737515943555401961
[发行人] CN=102737515943555401961
[序列号] 1E269224F694B808
[不是之前] 2016 年 2 月 9 日 16:24:59
[不是之后] 2026 年 8 月 31 日 16:24:59
[指纹] C26D88E95B402EFCC1AC81230F89C0B7887A6C6A
2021-11-22 15:25:58,445 |SimplicityOnlineWebApi.Commons.Utilities|INFO| - 创建服务 2021-11-22 15:25:58,455 |SimplicityOnlineWebApi.Commons.Utilities|INFO| - 服务已成功创建 Google.Apis.Drive.v3.DriveService
2021-11-22 15:25:58,528 |SimplicityOnlineWebApi.Commons.Utilities|INFO| - GetFiles 发生错误:指定的算法无效。
这里是GetFile方法的代码
Here is code:public override AttachmentFilesFolder GetFiles(DriveService service, string search)
{
AttachmentFilesFolder attachments = null;
IList<File> files = new List<File>();
try
{
//List all of the files and directories for the current user.
FilesResource.ListRequest list = service.Files.List();
list.PageSize = 100;
Utilities.WriteLog("Getting files");
files = list.Execute().Files;
}
catch (Exception ex)
{
Utilities.WriteLog("Error occured in GetFiles:" + ex.message);
}
}
【问题讨论】:
-
请完整的错误消息和堆栈跟踪。
-
请编辑您的问题并包含您的授权码以及错误消息。如果您认为这与 p12 文件有关,为什么不发布您的授权代码? TBH,您不应该使用 p12 文件,而应该将 Json 密钥文件用于服务帐户,这样可以更轻松地解决您现在遇到的问题。
-
GetFiles 是什么,我们可以看看那个方法吗?
-
您在 SimplicityOnlineWebApi.Commons.Utilities 中有一个名为 GetFiles 的方法向我们展示了这一点。
-
我已经提到了有错误的代码。这实际上是在 GetFile 方法中。我再次编辑了代码。请评价一下
标签: c# google-api google-drive-api google-api-dotnet-client service-accounts