首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何向Google Drive Api进行服务器到服务器的身份验证?

如何向Google Drive Api进行服务器到服务器的身份验证?
EN

Stack Overflow用户
提问于 2018-06-07 10:05:10
回答 1查看 821关注 0票数 0

我正在尝试在.Net框架中对Google Drive Api进行服务器到服务器的身份验证。

我在user authentication上见过很多例子,但也有几个server to server

我看到有这个Google Api library in C#,但我不知道如何使用它。

有人能帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-07 10:10:58

下面是一个通过API Client LibraryGoogle Drive API v3在.Net框架中使用MVC的示例

代码语言:javascript
复制
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);
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50731995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档