简体   繁体   中英

Google OAuth2 automatic sign in / automatic authentication

I'm wondering if there is a possible way to automatically sign-in to account? I'm using google oauth2 to access google drive, and every time I want to use it there's small window appearing to sign in. I would like to stay always signed in. Can I do this using php/js? Only administrator will use this feature.

Edit.

Basically i have a plugin for GDrive and its working fine. I would like to automatic call other method before just to sign in to google and it should be stored in session. Is it possible?

Assuming that you have access to the drive account you wish to access then you should consider using a service account. A service account is like a dummy user it has its own drive account. If you take the service account email address and share the files or directory's you want it to be able to access it will be preauthorized.

Services accounts only work server sided languages.

require_once __DIR__ . '/vendor/autoload.php';
// Use the developers console and download your service account
// credentials in JSON format. Place the file in this directory or
// change the key file location if necessary.
putenv('GOOGLE_APPLICATION_CREDENTIALS='.__DIR__.'/service-account.json');
/**
 * Gets the Google client refreshing auth if needed.
 * Documentation: https://developers.google.com/identity/protocols/OAuth2ServiceAccount
 * Initializes a client object.
 * @return A google client object.
 */
function getGoogleClient() {
    return getServiceAccountClient();
}
/**
 * Builds the Google client object.
 * Documentation: https://developers.google.com/api-client-library/php/auth/service-accounts
 * Scopes will need to be changed depending upon the API's being accessed. 
 * array(Google_Service_Analytics::ANALYTICS_READONLY, Google_Service_Analytics::ANALYTICS)
 * List of Google Scopes: https://developers.google.com/identity/protocols/googlescopes
 * @return A google client object.
 */
function getServiceAccountClient() {
    try {   
        // Create and configure a new client object.        
        $client = new Google_Client();
        $client->useApplicationDefaultCredentials();
        $client->addScope([YOUR SCOPES HERE]);
        return $client;
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
}

ServiceAccount.php

You can use the server side sign in for Google. You can read more about it here

Basically you fetch the access_token for the google account and store that token along with the refresh_token. You can use these tokens to fetch from drive any number of times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM