我尝试在c#中的代码示例中找到如何连接和从中提取数据的示例。有没有人知道我在哪里可以找到一个代码示例,或者如果一个人可以在这里添加一个简单的答案。我们会非常感激的
发布于 2022-04-01 11:14:25
构建在HTTP和JSON上,因此任何标准的HTTP客户机都可以向其发送请求并解析响应。
但是,Google客户端库提供了更好的语言集成、更好的安全性和对需要用户授权的调用的支持。客户端库可以使用多种编程语言;通过使用它们,可以避免手动设置HTTP请求和解析响应的需要。
您可以使用以下引用:
https://developers.google.com/api-client-library/dotnet/get_started
这是一个使用API的C#示例代码:
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Books.v1;
using Google.Apis.Books.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
namespace Books.ListMyLibrary
{
/// <summary>
/// Sample which demonstrates how to use the Books API.
/// https://developers.google.com/books/docs/v1/getting_started
/// <summary>
internal class Program
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Books API Sample: List MyLibrary");
Console.WriteLine("================================");
try
{
new Program().Run().Wait();
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
Console.WriteLine("ERROR: " + e.Message);
}
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
private async Task Run()
{
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { BooksService.Scope.Books },
"user", CancellationToken.None, new FileDataStore("Books.ListMyLibrary"));
}
// Create the service.
var service = new BooksService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Books API Sample",
});
var bookshelves = await
service.Mylibrary.Bookshelves.List().ExecuteAsync();
}
}
}在此示例代码中,通过调用UserCredential方法创建一个新的GoogleWebAuthorizationBroker.AuthorizeAsync实例。
如果您想为ASP.Net核心项目使用API (我们主要使用它),您可以查看以下页面:
https://www.nuget.org/packages/Google.Apis.Auth.AspNetCore3/
https://stackoverflow.com/questions/71705677
复制相似问题