In this tutorial, I will show you how to use C # with Google A.P.I. on Linux.
You will need to set an authentication key in your google account, see the credentials page.
I used Fedora Linux to install the NuGet command:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | [root@desk mythcat]# dnf install nuget Last metadata expiration check: 1:18:10 ago on Sat 06 Feb 2021 10:32:19 PM EET. Dependencies resolved. ================================================================================ Package Architecture Version Repository Size ================================================================================ Installing: nuget x86_64 2.8.7-11.fc33 fedora 419 k Transaction Summary ================================================================================ Install 1 Package Total download size: 419 k Installed size: 1.6 M Is this ok [y/N]: y ... Installed: nuget-2.8.7-11.fc33.x86_64 Complete! |
Use this command to install it:
1 2 3 4 5 6 7 8 9 10 11 12 13 | [mythcat@desk CSharpProjects]$ nuget install Google.Apis.Discovery.v1 Attempting to resolve dependency 'Google.Apis (= 1.10.0)'. Attempting to resolve dependency 'Google.Apis.Core (≥ 1.10.0)'. Attempting to resolve dependency 'Microsoft.Bcl (≥ 1.1.10)'. Attempting to resolve dependency 'Microsoft.Bcl.Build (≥ 1.0.14)'. Attempting to resolve dependency 'Microsoft.Bcl.Async (≥ 1.0.168)'. Attempting to resolve dependency 'Microsoft.Bcl.Build (≥ 1.0.21)'. Attempting to resolve dependency 'Microsoft.Net.Http (≥ 2.2.29)'. Attempting to resolve dependency 'Newtonsoft.Json (≥ 7.0.1)'. Attempting to resolve dependency 'Zlib.Portable.Signed (≥ 1.11.0)'. Attempting to resolve dependency 'log4net (≥ 2.0.3)'. Attempting to resolve dependency 'Google.Apis (≥ 1.49.0)'. 'Google.Apis' already has a dependency defined for 'Google.Apis.Core'. |
Create a basic C# project and test it:
1 2 3 4 5 6 7 8 9 10 11 12 13 | [mythcat@desk CSharpProjects]$ mkdir booksAPI && cd booksAPI [mythcat@desk booksAPI]$ dotnet new console Getting ready... The template "Console Application" was created successfully. Processing post-creation actions... Running 'dotnet restore' on /home/mythcat/CSharpProjects/booksAPI/booksAPI.csproj... Determining projects to restore... Restored /home/mythcat/CSharpProjects/booksAPI/booksAPI.csproj (in 161 ms). Restore succeeded. [mythcat@desk booksAPI]$ dotnet run Hello World! |
Add Google A.P.I. to this project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [mythcat@desk booksAPI]$ dotnet add package Google.Apis.Discovery.v1 --version 1.49.0 Determining projects to restore... ... info : Installing Newtonsoft.Json 12.0.3. info : Installing Google.Apis.Core 1.49.0. info : Installing Google.Apis 1.49.0. info : Installing Google.Apis.Discovery.v1 1.49.0. info : Package 'Google.Apis.Discovery.v1' is compatible with all the specified frameworks in project '/home/mythcat/CSharpProjects/booksAPI/booksAPI.csproj'. info : PackageReference for package 'Google.Apis.Discovery.v1' version '1.49.0' added to file '/home/mythcat/CSharpProjects/booksAPI/booksAPI.csproj'. info : Committing restore... info : Writing assets file to disk. Path: /home/mythcat/CSharpProjects/booksAPI/obj/project.assets.json log : Restored /home/mythcat/CSharpProjects/booksAPI/booksAPI.csproj (in 8.86 sec). |
Change the default project source code with this example and add your Google key:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | using System; using System.Threading.Tasks; using Google.Apis.Discovery.v1; using Google.Apis.Discovery.v1.Data; using Google.Apis.Services; namespace booksAPI { class Program { [STAThread] static void Main(string[] args) { Console.WriteLine("Discovery API Sample"); 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() { // Create the service. var service = new DiscoveryService(new BaseClientService.Initializer { ApplicationName = "Discovery Sample", ApiKey="...", }); // Run the request. Console.WriteLine("Executing a list request..."); var result = await service.Apis.List().ExecuteAsync(); // Display the results. if (result.Items != null) { foreach (DirectoryList.ItemsData api in result.Items) { Console.WriteLine(api.Id + " - " + api.Title); } } } } } |
I used my key and this is the result of the run project:
1 2 3 4 5 6 7 8 | [mythcat@desk booksAPI]$ dotnet run Discovery API Sample ==================== Executing a list request... abusiveexperiencereport:v1 - Abusive Experience Report API acceleratedmobilepageurl:v1 - Accelerated Mobile Pages (AMP) URL API accessapproval:v1 - Access Approval API accesscontextmanager:v1beta - Access Context Manager API |