如何将 .NET Core 连接到 Google 个联系人
How to connect .NET Core to Google contacts
我想根据数据库编辑 google 个联系人。我已经完成了所有的工作,唯一剩下的就是实现 API,但我就是不知道如何去做。我已经在 Google API 仪表板 中创建了一个应用程序并创建了一个 OAuth2.0 客户端 (我什至不确定这是否是我需要的) .
我已经下载了 Google 个程序集并将它们包含在我的 C# 控制台应用程序中。
- 如何从我的控制台应用程序连接到 google 个联系人?
- 如何在联系人列表中添加和删除条目?
可以在“已安装的应用程序”部分下HERE找到答案。以下是摘录:
Sample code using the Books API:
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();
...
}
}
}
In this sample code a new UserCredential instance is created by calling the GoogleWebAuthorizationBroker.AuthorizeAsync method. This
static method gets the following:
- The client secret (or a stream to the client secret).
- The required scopes.
- The user identifier.
- The cancellation token for cancelling an operation.
- An optional data store. If the data store is not specified, the default is a FileDataStore with a default Google.Apis.Auth folder. The
folder is created in Environment.SpecialFolder.ApplicationData.
The UserCredential that is returned by this method is set as a HttpClientInitializer on the BooksService (using the initializer). As
explained above, UserCredential implements an HTTP client initializer.
Notice that in the above sample code, the client secret information is loaded from a file, but you can also do the following:
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "PUT_CLIENT_ID_HERE",
ClientSecret = "PUT_CLIENT_SECRETS_HERE"
},
new[] { BooksService.Scope.Books },
"user",
CancellationToken.None,
new FileDataStore("Books.ListMyLibrary")
);
我想根据数据库编辑 google 个联系人。我已经完成了所有的工作,唯一剩下的就是实现 API,但我就是不知道如何去做。我已经在 Google API 仪表板 中创建了一个应用程序并创建了一个 OAuth2.0 客户端 (我什至不确定这是否是我需要的) .
我已经下载了 Google 个程序集并将它们包含在我的 C# 控制台应用程序中。
- 如何从我的控制台应用程序连接到 google 个联系人?
- 如何在联系人列表中添加和删除条目?
可以在“已安装的应用程序”部分下HERE找到答案。以下是摘录:
Sample code using the Books API:
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(); ... } } }
In this sample code a new UserCredential instance is created by calling the GoogleWebAuthorizationBroker.AuthorizeAsync method. This static method gets the following:
- The client secret (or a stream to the client secret).
- The required scopes.
- The user identifier.
- The cancellation token for cancelling an operation.
- An optional data store. If the data store is not specified, the default is a FileDataStore with a default Google.Apis.Auth folder. The folder is created in Environment.SpecialFolder.ApplicationData.
The UserCredential that is returned by this method is set as a HttpClientInitializer on the BooksService (using the initializer). As explained above, UserCredential implements an HTTP client initializer.
Notice that in the above sample code, the client secret information is loaded from a file, but you can also do the following:
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( new ClientSecrets { ClientId = "PUT_CLIENT_ID_HERE", ClientSecret = "PUT_CLIENT_SECRETS_HERE" }, new[] { BooksService.Scope.Books }, "user", CancellationToken.None, new FileDataStore("Books.ListMyLibrary") );