Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

How to use google api for wordpress login

I’m also working on plugin which uses google oAuth and YouTube data API. The functionality works on backend, admin logs in just once using his gmail credentials and then with use of refresh_token we can keep him logged in.

Keep in mind the refresh token is generated only the first time you give access
to your app. You have to clear your permissions to fetch it
again.

I used PHP and not javascript, but still here is my complete function to give you a brief idea

<?php
public function uty_main_page(){
    require_once ('config.php');
    global $wpdb;
    if(isset($_GET['code'])) {
        $client->authenticate($_GET['code']);
        $_SESSION['token'] = $client->getAccessToken();
        $token_decode = json_decode($_SESSION['token']);
        update_option( 'uty_refresh_token' , $token_decode->refresh_token );
        $client->setAccessToken($_SESSION['token']);
        echo 'Authorization Successful';
    }
    ?>
                    <?php if(get_option( 'uty_refresh_token' ) != ''){ ?>
                        <li><a href="#uty_upload">Upload</a></li>
                        <li><a href="#uty_videos">All Videos</a></li>
                    <?php } ?>
                    <li><a href="#uty_settings">Settings</a></li>
                </ul>
            </div>
            <div class="tab_content">
                <?php if( $client->isAccessTokenExpired() ) { ?>
                    <?php if(get_option( 'uty_refresh_token' ) != ''){ ?>
                        <?php
                        $client->refreshToken( get_option( 'uty_refresh_token' ) );
                        $_SESSION['token'] = $client->getAccessToken();
                        $client->setAccessToken($client->getAccessToken());
                        ?>
                        <div id="uty_upload">
                            <?php
                            if(isset($_POST['uty_video_submit'])){
                                if(isset($_FILES['uty_video'])){
                                    $uploaded = media_handle_upload('uty_video', 0);
                                    if(is_wp_error($uploaded)){
                                        $message = $uploaded->get_error_message();
                                    }
                                    else{
                                            //Do Some coding here
                                    }
                                }
                            }
                            ?>
                        </div>
                    <?php } ?>
                <?php } ?>
                <div id="uty_settings">
                    <?php if(isset($_POST['revoke-access-token'])){ ?>
                        <?php update_option( 'uty_refresh_token' , '' ); ?>
                        <?php update_option( 'uty_google_client_api' , '' ); ?>
                        <?php update_option( 'uty_google_client_id' , '' ); ?>
                        <?php update_option( 'uty_google_client_secret' , '' ); ?>
                        <?php update_option( 'uty_youtube_channel' , '' ); ?>
                    <?php } ?>
                    <h3>Please enter the below details to kick start your Youtube uploads</h3>

                    <?php if( !empty(get_option('uty_google_client_api')) && !empty(get_option('uty_google_client_id')) && !empty(get_option('uty_google_client_secret')) && empty(get_option('uty_refresh_token')) ){ ?>
                            <?php $state = mt_rand(); ?>
                            <?php $client->setState($state); ?>
                            <?php $_SESSION['state'] = $state; ?>
                            <?php $authUrl = $client->createAuthUrl(); ?>
                            <a href="<?php echo $authUrl; ?>">Click here to authorize your credentials</a>
                    <?php } elseif( !empty(get_option('uty_google_client_api')) && !empty(get_option('uty_google_client_id')) && !empty(get_option('uty_google_client_secret')) && !empty(get_option('uty_refresh_token')) ){ ?>

                    <?php } ?>
                </div>
            </div>
        </div>
    </div>
<?php } ?>

In the above function the main thing i’ve done is to store refresh_token when user logs in first time and then pass it in function to get new access token every time.

$client->refreshToken( get_option( 'uty_refresh_token' ) );
$_SESSION['token'] = $client->getAccessToken();
$client->setAccessToken($client->getAccessToken());

These three lines does the work of getting new access token every time.
Hope this helps you

Related Posts:

  1. How to implement Google reCaptcha without installing a plugin?
  2. Custom Connect to Facebook, problem logging in/logging out
  3. What would be the best way to implement Magic Link logins in WordPress?
  4. For using google api is it necessary to install the google client libraries for using Oauth 2 in wordpress installation?
  5. WordPress Google Calendar Oath 404
  6. Best option to implement external register/login to WP from self-made API
  7. Google credentials and redirect URI for Google OAuth2 in a WordPress plugin, questions
  8. Get WordPress login functions without printing anything
  9. Set logged in user based on API response
  10. Login and register by API
  11. In Django, how do I know the currently logged-in user?
  12. Disallow user from editing their own profile information
  13. wp_signon returns user, but the user is not logged in
  14. What exactly is ReAuth?
  15. What are the differences between wp_users and wp_usermeta tables?
  16. How can I send a welcome email to a user AFTER they login for the first time?
  17. Can not login with correct username and password
  18. How to remove the WordPress logo from login and register page?
  19. How do I use the WP REST API plugin and the OAuth Server plugin to allow for registration and login?
  20. Stop users from logging in from multiple locations
  21. My login form does not work
  22. Cookie settings for session across WPML subdomains using custom AJAX login
  23. Change Login Page for a Multisite Subsite
  24. Change admin login URL
  25. Is there a way to remove wp-login.php alias (login)?
  26. Login with cookie but without using WordPress code
  27. wp-admin redirecting to wp-login.php
  28. Get wordpress stored cookies for custom login
  29. Restrict PDF links
  30. Can’t access login page after password reset
  31. Remote REST request to check login status
  32. How can I test the login for an expired session?
  33. Rendering of custom walker menu function not working properly
  34. Force user to login in home page
  35. Login as a client no dashboard menu?
  36. Allow access to a page for admins only
  37. How to edit comment_reply_link
  38. Right practice to edit WP reset password email
  39. deny IPs from wp-login using .htaccess
  40. How To Change Wp Register/Login URL Permanently To My Custom Page
  41. How can I prevent my custom form from redirecting to wp-login?
  42. Custom Login iframe doesn’t work
  43. wp-admin returns 404 [closed]
  44. Can’t login to WordPress without hitting the Remember Me checkbox. Potential SSL implementation bi-product
  45. disable site_url redirect in wp-login.php
  46. Forgot password needs to redirect from wp-login to a custom page
  47. Is it possible to designate the login.php as homepage?
  48. Redirect login page except on reset pass
  49. Redirect users after failed login attempt?
  50. How to secure files based on format and word in file name with wp-config or htaccess?
  51. Redirect to custom variable
  52. Get the url of custom login page in the registration page
  53. WordPress error on log out ‘Not Permitted’ and can’t log out
  54. How can I change the email sender name from wordpress to (myblogname) on the “lost password” email?
  55. Changed primary domain and now wordpress login won’t work
  56. Add logout link when logged in, make it disappear when logged out?
  57. Keep user session with custom implementation of user login
  58. Forcing frontend login with UI switch
  59. Sidebar login widget with error print, returns an error
  60. Login without Password
  61. Exclude login page from custom maintenance code
  62. Temporarily locked out … how long is a “short while”?
  63. Change default login auth
  64. I can’t access my WordPress dashboard – shows Warning message [closed]
  65. Add html to wp_login_form
  66. Are login functions considered part of the WP backend?
  67. Can I protect a type of content site-wide with a single password?
  68. Sign in with social media accounts without creating a WP account [closed]
  69. WordPress login form script
  70. WordPress authentication using custom php?
  71. what is the best and safest way to allow users to register to site
  72. ERROR: Cookies are blocked due to unexpected output (no FTP access)
  73. Sharing a logged in session with a custom subdmain site?
  74. How do I change the language of the login page to Arabic?
  75. MAMP localhost wordpress site, not possible to sign in, username and password are correct
  76. Login issues wordpress page
  77. How to post frontend login form to a different authentication script from wp-login?
  78. Make WordPress User Name be the Company Name when Registering (not the default ‘first name’ last name’ email address’)
  79. Why do I have a reauth=1 redirection loop when I try to log in to WordPress hosted on AWS Fargate?
  80. Problem with is_user_logged_in() function in some pages
  81. Confused – can’t access wordpress dashboard or site that has been published
  82. wordpress login loop and session problem
  83. unable to Login to Admin
  84. WordPress login loop on proxy setup with SSL – Apache proxy to IIS
  85. WordPress login page blank after customizations – works on other sites
  86. WordPress does not send email confirmation to newly registered users
  87. confirmation email is send from my local host registration of a user but mail will not display in there email account?
  88. Server error after log in
  89. How do I resolve my inability to login to WordPress dashboard? [duplicate]
  90. Chrome incorrectly displaying WP login as ‘not fully secure’
  91. Changed from HTTP to HTTP, can login no longer login
  92. Hide wp-login.php but not the widget
  93. Without user loging inner page is disable wordpress [duplicate]
  94. Registration and Login form
  95. WordPress and Magento: let WordPress manage user registration and logins?
  96. Share login status across subdomains without network
  97. How to change the login URL
  98. Click on banner to register to the blog
  99. Must Log In to Visit Site [duplicate]
  100. Customize From and email address on password reset
Categories login Tags api, google, login, oauth
Passing form data on submit
How do I hide submenus from the top menu bar?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress