我正在尝试在.Net框架中对Google Drive Api进行服务器到服务器的身份验证。
我在user authentication上见过很多例子,但也有几个server to server。
我看到有这个Google Api library in C#,但我不知道如何使用它。
有人能帮我吗?
发布于 2018-06-07 10:10:58
下面是一个通过API Client Library和Google Drive API v3在.Net框架中使用MVC的示例
using System;
using System.Collections.Generic;
using System.IO;
using System.Web.Mvc;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using static Google.Apis.Drive.v3.DriveService;
namespace TestApiGoogle.Controllers
{
public class HomeController : Controller
{
public ActionResult GoogleAuth()
{
FileStream fsSource = new FileStream
(@"Path\secret_info.json", FileMode.Open, FileAccess.Read);
string[] Scopes = { Scope.Drive };
string ApplicationName = "Your app name";
GoogleCredential credential = GoogleCredential.FromStream(fsSource)
.CreateScoped(Scopes);
// Create Drive API service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
FilesResource.ListRequest listRequest = service.Files.List();
listRequest.PageSize = 10;
listRequest.Fields = "nextPageToken, files(id, name)";
// List files.
IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
.Files;
if (files != null && files.Count > 0)
{
foreach (var file in files)
{
Console.WriteLine("{0} ({1})", file.Name, file.Id);
}
}
else
{
Console.WriteLine("No files found.");
}
var jsonObject = new
{
files
};
return Json(jsonObject, JsonRequestBehavior.AllowGet);
}
}
}
https://stackoverflow.com/questions/50731995
复制相似问题