The samples from these recent articles use the OAuth2 component to perform client authentication.
The OAuth2 component is a thin wrapper for Google's GAPI library. To use the OAuth2 class, create an instance of it passing in the following parameters:
Once the OAuth2 class is created, you can:
For example, this is how the samples we discussed earlier use the OAuth2 class:
// instantiate the OAuth2 class
const SCOPES = [ '[https://www.googleapis.com/auth/userinfo.email](https://www.googleapis.com/auth/userinfo.email)' ]
let auth = new OAuth2(API_KEY, CLIENT_ID, SCOPES);
// click button to log user in or out
oAuthBtn.addEventListener('click', () => {
if (auth.user) {
auth.signOut();
} else {
auth.signIn();
}
});
// update button/sheet state when user changes
auth.userChanged.addHandler(s => {
// update button caption
let user = s.user;
oAuthBtn.textContent = user ? 'Sign Out' : 'Sign In';
// update GoogleSheet and Firestore access token
gsNWind.accessToken = user ? s.accessToken : null;
fsNWind.accessToken = user ? s.accessToken : null;
});
As you can see, the OAuth2 class makes user authentication very easy to use. But under the covers, it is the GAPI library and Google's OAuth services that are doing all the heavy lifting.
The main service we get from the OAuth2 class is the accessToken string that the GoogleSheets and Firestore classes need for authorization. If you want to use other authorization services that can provide the access tokens, you don't need the OAuth2 class.
Try the Live Demos