Search⌘ K
AI Features

Generate API Key

Explore the process of generating an API key for the Blogger API using OAuth credentials. Learn how to sign in, authorize the app, obtain the access token, and save it for making authenticated API requests effectively.

We'll cover the following...

Now, we'll use the OAuth credentials we generated in the previous lesson to generate the access token required to make API calls.

Generate access token

Step 1: Click the "Edit" button in the widget below and enter the "Client ID" we copied in the previous lesson in the textbox. Then, click the "Save" button.

Step 2: Click the "Run" button to start the application. Then, click the URL next to "Your app can be found at:" in the widget below to open the application in a new tab.

import React, { useState, useEffect } from 'react';
import { GoogleLogin, GoogleLogout } from 'react-google-login';
import { gapi } from 'gapi-script';
import { ReactSession } from 'react-client-session';
import { useNavigate } from 'react-router-dom';

function OAuth() {
    const [ profile, setProfile ] = useState('');
    const clientId = "{{CLIENT_ID}}";
    const [alertText , setAlertText] = useState('');
    useEffect(() => {
        const initClient = () => {
            gapi.client.init({
                clientId: clientId,
                scope: "https://www.googleapis.com/auth/blogger https://www.googleapis.com/auth/blogger.readonly"
            });
        };
        gapi.load('client:auth2', initClient);
    });

    const onSuccess = (res) => {
        setProfile(res.profileObj);
        ReactSession.set("ACCESS_TOKEN", res.tokenObj.access_token);
        ReactSession.set("PROFILE", profile);
    };

    const onFailure = (err) => {
        setAlertText('Oops! Login failed!');
        console.log('Error: ', err);
    };

    const navigate = useNavigate();
    
    const SignOut = () => {
        setProfile(null);
        ReactSession.set("ACCESS_TOKEN", null);
        ReactSession.set("PROFILE", null);
        ReactSession.clear();
        navigate('/');
    }

    return (
        <div className="d-flex flex-column h-100 bg-dark">
            <main className="flex-shrink-0">
                <div className="container col-10 text-center border border-secondary rounded min-vh-100 bg-white">
                    <div className="row justify-content-center">
                        <div className="col-5 mt-4"> 
                            <img src="/blogger-full.png" alt="Blogger API" className="card-img-top mb-5" />
                            <br /><br />
                            {ReactSession.get("ACCESS_TOKEN") ? (
                            <React.Fragment>
                                <div class="input-group mb-3">
                                    <label class="input-group-text" for="accessToken">Access Token</label>
                                    <input type="text" class="form-control" aria-label="Access Token" id="accessToken" aria-describedby="button-addon2" readOnly value={ReactSession.get("ACCESS_TOKEN")} />
                                    <button class="btn btn-outline-secondary" type="button" id="button-addon2" onClick={() => {navigator.clipboard.writeText(ReactSession.get("ACCESS_TOKEN"));}}>Copy</button>
                                </div>
                                <br /><br />
                                <GoogleLogout 
                                    clientId={clientId} 
                                    buttonText="Sign out" 
                                    onLogoutSuccess={SignOut} 
                                />
                            </React.Fragment>
                            ) : (
                                <React.Fragment>
                                    {alertText &&
                                        <div class={`alert alert-danger`} role="alert">
                                            {alertText}
                                        </div>
                                    }
                                    <br />
                                    <GoogleLogin
                                        clientId={clientId}
                                        buttonText="Sign in with Google"
                                        onSuccess={onSuccess}
                                        onFailure={onFailure}
                                        cookiePolicy={'single_host_origin'}
                                        isSignedIn={true}
                                    />
                                </React.Fragment>
                            )}
                        </div>
                    </div>
                </div>
            </main>
        </div>
    );
}

export default OAuth;
Generate access token

Step 3: After the application starts, we see a Google sign-in button on the homepage. Click the button and sign in using your Google account credentials.

Step 4 (only for first-time sign-ins): Authorize the app after signing in to your Google account. Click the "Allow" button.

Step 5: We now see the access token on the screen. Copy the access token and click the "Edit" button in the widget below. Enter the token in its textbox and click the "Save" button to save it as an API key.

Step 6: Click the "Run" button to verify the access token.

Javascript (babel-node)
// Validating access token
validateToken('{{ACCESS_TOKEN}}');

Note: The ACCESS_TOKEN expires after 3,600 seconds. So, if a 401:Unauthorized error occurs, feel free to return to this lesson and generate a new ACCESS_TOKEN.

Now that you've set up your API key, you're ready to make your first call to the Blogger API!