簡體   English   中英

Google OAuth2自動登錄/自動身份驗證

[英]Google OAuth2 automatic sign in / automatic authentication

我想知道是否有可能自動登錄帳戶? 我正在使用google oauth2來訪問google驅動器,每次我要使用它時,都會出現一個小窗口登錄。我想始終保持登錄狀態。我可以使用php / js嗎? 只有管​​理員可以使用此功能。

編輯。

基本上我有一個適用於GDrive的插件,並且可以正常工作。 我想在登錄Google之前自動調用其他方法,並且應該將其存儲在會話中。 可能嗎?

假設您有權訪問要訪問的驅動器帳戶,則應考慮使用服務帳戶。 服務帳戶就像虛擬用戶一樣,擁有自己的驅動器帳戶。 如果您使用服務帳戶的電子郵件地址並共享文件或目錄,則希望它能夠訪問它。

服務帳戶只能使用服務器端的語言。

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

您可以使用Google的服務器端登錄。 您可以在這里了解更多信息

基本上,您獲取Google帳戶的access_token並將該令牌與refresh_token一起存儲。 您可以使用這些令牌多次從驅動器獲取。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM