introduction to the google apis client library for .net

Download Introduction to the Google APIs Client Library for .NET

If you can't read please download the document

Upload: eyal-peled

Post on 16-Apr-2017

4.356 views

Category:

Technology


2 download

TRANSCRIPT

Introduction to the .NET client librarygo/apiarydotnetintro

https://code.google.com/p/google-api-dotnet-client/Eyal Peled peleyal@

AgendaRestHttp LayerClient ServiceService RequestMedia OAuth2PCL3rd parties

DiscoveryCode Generator

https://code.google.com/p/google-api-dotnet-client/

AgendaRestHttp LayerClient ServiceService RequestMedia OAuth2PCL3rd parties

DiscoveryCode Generator

https://code.google.com/p/google-api-dotnet-client/

Google APIs .NET REST Representational State Transfer

Client and servers transfer resource representations

The set of verbs in a REST APIis typically limited to CRUD

See Google API Explorer for linksto all Google API Rest services

https://code.google.com/p/google-api-dotnet-client/

AgendaRestHttp LayerClient ServiceService RequestMedia OAuth2PCL3rd parties

DiscoveryCode Generator

https://code.google.com/p/google-api-dotnet-client/

Google APIs .NET Http LayerHttpClientA simple programming interface to code against - SendAsync, GetAsync, etc.

It uses the new Task based asynchronous methods, which makes writing responsive and performant UI applications across all platforms a lot simpler.

Targets Windows 7 and 8, WP, etc. See PCL

Part of .NET 4.5 (not PCL) and available for .NET 4.0 developers as a PCL (using NuGet)

https://code.google.com/p/google-api-dotnet-client/

https://code.google.com/p/google-api-dotnet-client/Google APIs .NET Http LayerHttpClientMessage handlers as a key architectural component

Every request is passed through a chain of handlers (derive from HttpMessageHandler)

HttpClientHandler uses

System.Net.HttpWebRequest

andSystem.Net.HttpWebResponse

under the cover, and provides a great abstraction.

Google APIs .NET Http Layer

ConfigurableMessageHandlerThe main HTTP logic IHttpExecuteInterceptorInterceptor before request is made IHttpUnsuccessfulResponseHandlerHandler for abnormal HTTP response IHttpExceptionHandlerHandler for an exception during a request

https://code.google.com/p/google-api-dotnet-client/

Google APIs .NET Http Layer

ws

https://code.google.com/p/google-api-dotnet-client/

ConfigurableHttpClient

Inherits from HttpClient

Contains the handler

Google APIs .NET Http Layer

https://code.google.com/p/google-api-dotnet-client/

HttpClientFactory

Constructs a new HttpClient

Great for mocking using CreateHandler

(By default creates HttpClientHandler)

Google APIs .NET Http Layer

https://code.google.com/p/google-api-dotnet-client/

BackOffHandler

Implements exception and unsuccessful response handlers

Contains a BackOff logic

AgendaRestHttp LayerClient ServiceService RequestMedia OAuth2PCL3rd parties

DiscoveryCode Generator

https://code.google.com/p/google-api-dotnet-client/

Google APIs .NET Client Service

https://code.google.com/p/google-api-dotnet-client/

IClientService

Serialization

GZip support

Authenticator

(Will be removed after a new Auth library will be created)

Google APIs .NET Client Service

https://code.google.com/p/google-api-dotnet-client/

BaseClientService

Thread-safe uses Initializer

Expoenential back-off policy

Api Key

GZip Enabled

Client Factory

Google APIs .NET Client Service

Generated services inherit from BaseClientSerivceThey implement resource related properties

They contain resources and methods based on Discovery

Follow DriveService implementation in the next slide...https://code.google.com/p/google-api-dotnet-client/

Google APIs .NET Client Service

public class DriveService : Google.Apis.Services.BaseClientService { public DriveService(Google.Apis.Services.BaseClientService.Initializer initializer) : base(initializer) { about = new AboutResource(this); files = new FilesResource(this); ... }

public override string Name { get { return "drive"; } } public override string BaseUri { get { return "https://www.googleapis.com/drive/v2/"; } } public override string BasePath { get { return "drive/v2/"; } } public enum Scopes { /// View and manage the files and documents in your Google Drive [Google.Apis.Util.StringValueAttribute("https://www.googleapis.com/auth/drive")] Drive, /// View and manage its own configuration data in your Google Drive [Google.Apis.Util.StringValueAttribute("https://www.googleapis.com/auth/drive.appdata")] DriveAppdata,/// View your Google Drive apps }https://code.google.com/p/google-api-dotnet-client/

AgendaRestHttp LayerClient ServiceService RequestMedia OAuth2PCL3rd parties

DiscoveryCode Generator

https://code.google.com/p/google-api-dotnet-client/

Google APIs .NET Service Request

https://code.google.com/p/google-api-dotnet-client/

ClientServiceRequest

ETag support

based TPL

Execute \ ExecuteAsyncExecuteAsStream \ ExecuteAsStreamAsync

Google APIs .NET Service Request

Generated requests inherit from ClientServiceRequestThey Implement the concrete HttpMethod, RestPath, etc.

They are executed by calling to Execute (or ExecuteAsync)

Follow GetRequest (File) implementation in the next slide...https://code.google.com/p/google-api-dotnet-client/

Google APIs .NET Service Request

public class GetRequest : DriveBaseServiceRequest { public GetRequest(Google.Apis.Services.IClientService service, string fileId) : base(service) { FileId = fileId; InitParameters(); } [Google.Apis.Util.RequestParameterAttribute("fileId", Google.Apis.Util.RequestParameterType.Path)] public virtual string FileId { get; private set; }

[Google.Apis.Util.RequestParameterAttribute("projection", Google.Apis.Util.RequestParameterType.Query)] public virtual System.Nullable Projection { get; set; }

public override string MethodName { get { return "get"; } } public override string HttpMethod { get { return "GET"; } } public override string RestPath { get { return "files/{fileId}"; } }

}https://code.google.com/p/google-api-dotnet-client/

AgendaRestHttp LayerClient ServiceService RequestMedia OAuth2PCL3rd parties

DiscoveryCode Generator

https://code.google.com/p/google-api-dotnet-client/

Google APIs .NET MediaResumable Media Upload

https://code.google.com/p/google-api-dotnet-client/

The protocol

Recovery server errors

Chunk size

Progress changed event

Upload

UploadAsync

Google APIs .NET MediaMedia Downloader

https://code.google.com/p/google-api-dotnet-client/

Chunk size

ProgressChanged event

Download

DownloadAsync

AgendaRestHttp LayerClient ServiceService RequestMedia OAuth2PCL3rd parties

DiscoveryCode Generator

https://code.google.com/p/google-api-dotnet-client/

Google APIs .NET OAuth2

https://code.google.com/p/google-api-dotnet-client/

In the traditional client-server authentication model, the client requests an access-restricted resource (protected resource) on the server by authenticating with the server using the resource owner's credentials. OLDIn order to provide third-party applications access to restricted resources, the resource owner shares its credentials with the third partyNEWresource owner - An entity capable of granting access to a protected resource. When the resource owner is a person, it is referred to as an end-user.resource server (GOOGLE) - The server hosting the protected resources, capable of accepting and responding to protected resource requests using access tokens.client - An application making protected resource requests on behalf of the resource owner and with its authorization.authorization server (GOOGLE) - The server issuing access tokens to the client after successfully authenticating the resource owner and obtaining authorization.

Google APIs .NET OAuth2

https://code.google.com/p/google-api-dotnet-client/Google OAuth2 documentationGoogle API ConsoleSupport installed and web applicationsSupport service accountCurrent implementation IAuthenticator to apply authorization header to a request

Usage of DotNetOpenAuth

Implementation is in Google.Apis.Authentication.OAuth2

Google APIs .NET OAuth2

https://code.google.com/p/google-api-dotnet-client/RoadmapRemove DotNetOpenAuth

Create a Google.Apis.Auth PCL

Create concrete implementation for

Google.Apis.Auth.DotNet4 (installed applications)

Google.Apis.Auth.Mvc4 (web applications)

Google.Apis.Auth.WP (Windows Phone)

Google.Apis.Auth.WinRT (Windows 8 applications)

The main reason for creating a specific assembly for a platform is based on the way we read the authorization code.

AgendaRestHttp LayerClient ServiceService RequestMedia OAuth2PCL3rd parties

DiscoveryCode Generator

https://code.google.com/p/google-api-dotnet-client/

Google APIs .NET PCL

Portable Class Library

Current statusGoogle.Apis is PCL

The generated APIs are PCLs as well

RoadmapCreate a Google.Apis.Auth PCL library (see OAuth slides)

https://code.google.com/p/google-api-dotnet-client/

AgendaRestHttp LayerClient ServiceService RequestMedia OAuth2PCL3rd parties

DiscoveryCode Generator

https://code.google.com/p/google-api-dotnet-client/

Google APIs .NET 3rd partiesNuGet packages:Newtonsoft.Json 5.0.5

Json is the client's default data formatMicrosoft.Net.Http 2.1.10

We use HttpClient as our transport layer.Microsoft.Bcl.Async 1.10.16

Although we target .NET 4.0 we use async-awaitZlib.Portable 1.9.2

While GZip is enabled, we use this library to encode messages

log4net

The library's logging framework

https://code.google.com/p/google-api-dotnet-client/

AgendaRestHttp LayerClient ServiceService RequestMedia OAuth2PCL3rd parties

DiscoveryCode Generator

https://code.google.com/p/google-api-dotnet-client/

Google APIs .NET Discovery

Each Google API service exposes a discovery doc using the json format (e.g. https://www.googleapis.com/discovery/v1/apis/drive/v2/rest)

The service generator generates a service, resources, models and requests by this doc

Similar to wsdl

Read more about Discovery API here

https://code.google.com/p/google-api-dotnet-client/

AgendaRestHttp LayerClient ServiceService RequestMedia OAuth2PCL3rd parties

DiscoveryCode Generator

https://code.google.com/p/google-api-dotnet-client/

Google APIs .NET Code Generator

https://code.google.com/p/google-api-dotnet-client/From 1.5.0-beta we use Google APIs client generator (which is available here)

The .NET specific templates for 1.5.0-beta are available here.

The decision to move to Google APIs client generator was based on the following:

Share logic with other Google APIs client libraries

It is easier to use Django templates

It reduces library code significantly

One step forward supporting End Points

Resourcescode.google.com - WIKI

Google API .NET Announcements blog

StackOverflow google-api-dotnet-client tag

Google Group

Google API Explorer

Google API Console

https://code.google.com/p/google-api-dotnet-client/

Questions?

https://code.google.com/p/google-api-dotnet-client/