Search code examples
apioauthhttprequestpostmangoogle-oauth

How to get a Google OAuth 2 token for api testing


I am trying to build a simple node API that uses google OAuth 2.0 to authenticate endpoints, and I am trying to test it by making calls with Postman, but I cannot figure out how to get an OAuth token for my google account to use when calling the API. I tried using the OAuth feature in Postman but could not get it configured properly. Can anyone give me any guidance on how to either obtain a token I can use or properly use Postman/any other utility to do this testing?


Solution

  • Here's how you can use Google's Sign-In button template to initialize the login & grant of permissions process (provided the project is set up in the Google Developer Console):

    <meta name="google-signin-client_id" content="{{ OAUTH2_CLIENT_ID }}">
    
    <script src="https://apis.google.com/js/platform.js?onload=onLoad" async defer></script>
    
    <div id="google-signin-button"
         class="g-signin2"
         data-width="170"
         data-height="30"
         data-onsuccess="onSignIn"
         data-onfailure="onSignInFailure">
    </div>
    
    function onSignIn(googleUser) {
        var profile = googleUser.getBasicProfile();
        var idToken = googleUser.getAuthResponse().id_token;//this is your token
    }