OAuth2 è un metodo di autorizzazione comune utilizzato da diverse aziende come Google. Quando si desidera autenticarsi presso i server Google per utilizzare una qualsiasi delle loro API, di solito è richiesta un'autenticazione tramite OAuth2.
sgcWebSockets supporta OAuth2 sotto il client HTTP/2; è disponibile una proprietà chiamata Authentication.Token.OAuth a cui è necessario assegnare un'istanza di TsgcHTTP_OAuth2.
Per connettersi alle API Google, è necessario creare un'istanza di TsgcHTTP_OAuth2 e compilare i seguenti dati:
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';
Dopo aver configurato il componente client OAuth2, creare una nuova istanza di TsgcHTTP2Client e assegnare il componente OAuth2 al client HTTP/2.
TsgcHTTP2Client1.Authentication.Token.OAuth := TsgcHTTP_OAuth1;
Infine, effettuare una richiesta per ottenere un elenco dei messaggi dell'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;