c# - 如何通过多用户应用访问特定的 google drive 帐户?
问题描述
正如标题所说,如何使用位于可以安装在多台机器上的应用程序中的 Google API 来处理特定的 google drive 帐户。
亲切地。
解决方案
这是我使用 Google drive api 和身份验证的代码:
private void btnAffFiles_Click(object sender, EventArgs e)
{
List<IList<Google.Apis.Drive.v3.Data.File>> ListFiles = Manip_GDrive.GetListElem("",1000);
for (int i=0; i < ListFiles.Count; i++)
{
RTBFiles.Text = RTBFiles.Text + "\nPage N° : " + (int)(i+1);
if (ListFiles[i] != null && ListFiles[i].Count > 0)
{
foreach (var file in ListFiles[i])
{
Console.WriteLine("File Name : " + file.Name + " // File Id : " + file.Id);
RTBFiles.Text = RTBFiles.Text + "\nFile Name : " + file.Name + " // File Id : " + file.Id;
}
}
}
}
static string[] Scopes = { DriveService.Scope.Drive };
static string ApplicationName = "my-project";
public static List<IList<Google.Apis.Drive.v3.Data.File>> GetListElem(string parentFolderId, int pageSize)
{
List<IList<Google.Apis.Drive.v3.Data.File>> listAllElem = new List<IList<Google.Apis.Drive.v3.Data.File>>();
try
{
if (InternetCnxState() == true)
{
DriveService service = Manip_GDrive.UserCredentialService("client_id.json");
string pageToken = null;
do //loop for all pages
{
var listRequest = service.Files.List();
if (parentFolderId != "") { listRequest.Q = "'" + parentFolderId + "' in parents"; }
listRequest.Spaces = "drive";
listRequest.PageSize = pageSize;
listRequest.Fields = "nextPageToken, files(id, name)";
listRequest.PageToken = pageToken;
var result = listRequest.Execute();
listAllElem.Add(result.Files);
pageToken = result.NextPageToken;
} while (pageToken != null);
}
else
{
MessageBox.Show("Pas de connexion internet! \nVeuillez vérifier votre connexion internet avant de réessayer", "Hmidi Amine", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
catch (Exception e)
{
MessageBox.Show("Une erreure s'est produite! \nCode d'erreur : " + e.Message, "Hmidi Amine", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return listAllElem;
}
public static DriveService UserCredentialService(string streamJsonPath)
{
UserCredential credential;
using (var stream = new FileStream(streamJsonPath, FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
Scopes, "user", CancellationToken.None, new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Drive API service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
return service;
}
推荐阅读
- java - 由于缺少 bean,Spring Boot 应用程序无法启动
- data-structures - 哪种方式学习数据结构更有效?
- desktop-application - WPF应用程序中的乌尔都语
- javascript - If 子句括号和相等的优先级
- android - 除非图像 URL 是 HTTPS,否则 Fresco 无法在 Android 9(Pie) 中加载图像
- azure - 我可以更改应用服务计划/应用服务以使用应用服务环境吗
- r - 错误:`data` 和 `reference` 应该是具有相同级别的因子'不返回混淆矩阵
- python - Django:我们可以更改文件字段,以便在选择文件时立即上传
- node.js - 当我在浏览器中运行 firebase 功能时,出现错误:无法获取 /testingtest
- javascript - 如何从 Autodesk 模型衍生 API 的响应中识别提取的 obj 文件?