TsgcHTTP2Client | HTTP/2 and OAuth2

OAuth2 is a common authorization method uses by several companies like Google. When you want authenticate against google servers to use any of their APIs, usually requires an Authentication using OAuth2.

 

sgcWebSockets supports OAuth2 under HTTP/2 client, there is a property called Authentication.Token.OAuth where you must assign of TsgcHTTP_OAuth2.

 

How connect to GMail Google API

In order to connect to Google APIs, we will need to create an instance of TsgcHTTP_OAuth2 and fill the following data:

 


TsgcHTTP_OAuth1.AuthorizationServerOptions.AuthURL := 'https://accounts.google.com/o/oauth2/auth';
TsgcHTTP_OAuth1.AuthorizationServerOptions.TokenURL := 'https://accounts.google.com/o/oauth2/token';

TsgcHTTP_OAuth1.LocalServerOptions.IP := '127.0.0.1'; TsgcHTTP_OAuth1.LocalServerOptions.Port := 8080;
TsgcHTTP_OAuth1.OAuth2Options.ClientId := 'your client id'; TsgcHTTP_OAuth1.OAuth2Options.ClientSecret := 'your client secret';

 

After fill the OAuth2 client component, create a new instance of TsgcHTTP2Client and Assign the OAuth2 component to the HTTP/2 client.

 


 TsgcHTTP2Client1.Authentication.Token.OAuth := TsgcHTTP_OAuth1;

 

Finally, do a request to get a list of messages of account yourname@gmail.com

 


oStream := TStringStream.Create('');
Try
  TsgcHTTP2Client1.Get('https://gmail.googleapis.com/gmail/v1/users/yourname@gmail.com/messages', oStream);
  ShowMessage(oStream.DataString);
Finally
  oStream.Free;
End;