我创建了一个Web应用程序,并实现了Google登录功能。我想为它添加搜索功能,让用户可以搜索他/她的驱动器中存在的任何文件。在Google Drive API的示例代码中,应用程序打开浏览器窗口并提供凭据后,令牌被本地存储,我希望通过代码传递凭据,因此我为此创建了一个服务帐户。
在Google文档中写道:
在上述代码中,我应该在哪里传递模拟用户的电子邮件地址?我的应用程序使用 g-suite 帐户。我尝试将
在Google文档中写道:
您的应用程序现在拥有作为您域中的用户进行API调用的权限(“模拟”用户)。当您准备进行授权的API调用时,您需要指定要模拟的用户。
以下是我的代码-static void Main(string[] args)
{
try
{
var service = AuthenticateServiceAccountV1(GServiceAccount, "keycredentials.json");
FilesResource.ListRequest listRequest = service.Files.List();
listRequest.PageSize = 1000;
listRequest.Fields = "nextPageToken, files(webViewLink, name, id)";
IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute().Files;
Console.WriteLine("Processing...\n");
if (files != null && files.Count > 0)
{
var listfiles = files.Select(x => x.Id).ToList();
Console.WriteLine(files.Count + " records fetched.");
}
else
{
Console.WriteLine("No files found.");
}
Console.ReadLine();
}
catch(Exception ex)
{
throw ex;
}
}
static DriveService AuthenticateServiceAccountV1(string ServiceAccountEmail, string KeyFilePath)
{
try
{
if (string.IsNullOrEmpty(KeyFilePath))
throw new Exception("Path to the service account credentials file is required.");
if (!File.Exists(KeyFilePath))
throw new Exception("The service account credentials file does not exist at: " + KeyFilePath);
if (string.IsNullOrEmpty(ServiceAccountEmail))
throw new Exception("ServiceAccountEmail is required.");
if (Path.GetExtension(KeyFilePath).ToLower() == ".json")
{
GoogleCredential credential;
using (var stream = new FileStream(KeyFilePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream)
.CreateScoped(Scopes);
}
return new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
}
else if (Path.GetExtension(KeyFilePath).ToLower() == ".p12")
{
var certificate = new X509Certificate2(KeyFilePath, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(ServiceAccountEmail)
{
Scopes = Scopes,
}.FromCertificate(certificate));
return new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName
});
}
else
{
throw new Exception("Unsupported Service accounts credentials.");
}
}
catch (Exception ex)
{
return null;
}
}
在上述代码中,我应该在哪里传递模拟用户的电子邮件地址?我的应用程序使用 g-suite 帐户。我尝试将
credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes);
替换为 credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes).CreateWithUser("userid@gmail.com");
它会抛出异常,说:
{"Error:\"unauthorized_client\", Description:\"Client is unauthorized to retrieve access tokens using this method.\", Uri:\"\""}
CreateScoped
和CreateWithUser
的顺序。我不能解释为什么,但这对我起作用了。 - hultqvist