简体   繁体   English

Google OAuth2自动登录/自动身份验证

[英]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? 我正在使用google oauth2来访问google驱动器,每次我要使用它时,都会出现一个小窗口登录。我想始终保持登录状态。我可以使用php / js吗? Only administrator will use this feature. 只有管​​理员可以使用此功能。

Edit. 编辑。

Basically i have a plugin for GDrive and its working fine. 基本上我有一个适用于GDrive的插件,并且可以正常工作。 I would like to automatic call other method before just to sign in to google and it should be stored in session. 我想在登录Google之前自动调用其他方法,并且应该将其存储在会话中。 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 ServiceAccount.php

You can use the server side sign in for Google. 您可以使用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. 基本上,您获取Google帐户的access_token并将该令牌与refresh_token一起存储。 You can use these tokens to fetch from drive any number of times. 您可以使用这些令牌多次从驱动器获取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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