从php使用Google Drive API“无”身份验证屏幕

从php使用Google Drive API“无”身份验证屏幕,php,google-drive-api,google-oauth,google-api-php-client,service-accounts,Php,Google Drive Api,Google Oauth,Google Api Php Client,Service Accounts,这就是我面临的情况。 我有一个php驱动的web应用程序,我需要从中访问我的Google Drive帐户上的一些文件链接。我只需要访问我帐户中的文件,因为我存储的图像与我必须为不同客户提供的图像相同。 我试图查看GoogleDriveAPI文档,但我不需要OAuth和同意屏幕。该应用程序应该在后台访问我的Google Drive帐户,只是为了检索它必须向客户提供的一些信息,而不需要他们做任何事情 我在StackOverflow上读到了非常类似的问题,但这些都没有真正帮助我。。。一些人提到使用服务

这就是我面临的情况。 我有一个php驱动的web应用程序,我需要从中访问我的Google Drive帐户上的一些文件链接。我只需要访问我帐户中的文件,因为我存储的图像与我必须为不同客户提供的图像相同。 我试图查看GoogleDriveAPI文档,但我不需要OAuth和同意屏幕。该应用程序应该在后台访问我的Google Drive帐户,只是为了检索它必须向客户提供的一些信息,而不需要他们做任何事情

我在StackOverflow上读到了非常类似的问题,但这些都没有真正帮助我。。。一些人提到使用服务帐户,另一些人说它们在这方面没有用处


您能帮助我了解在这种情况下哪种方法是最好的吗?提前多谢

您正在寻找的是一个服务帐户。服务帐户是虚拟用户,他们实际上有自己的谷歌驱动器帐户。因为他们是用户,所以您可以像其他用户一样与他们共享文件。一旦该文件与服务帐户共享,它就可以访问该文件,然后可以像使用Oauth2一样使用该文件,当您仅登录时,您将不需要登录并同意访问,因为您已经配置了访问权限

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();
    }
}

你要找的是一个服务帐户。服务帐户是虚拟用户,他们实际上有自己的谷歌驱动器帐户。因为他们是用户,所以您可以像其他用户一样与他们共享文件。一旦该文件与服务帐户共享,它就可以访问该文件,然后可以像使用Oauth2一样使用该文件,当您仅登录时,您将不需要登录并同意访问,因为您已经配置了访问权限

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();
    }
}

如果使用服务帐户,我建议您在这里不必与它共享每个文件,您可以模拟自己并直接获取文件。非常感谢你们两位!我设法使用一个服务帐户通过客户端PHP库访问我的驱动器,现在我面临另一个问题。。。如何列出或读取已从我的帐户与服务帐户共享的文件的某些属性?例如,我可以通过服务帐户将文件上载到共享目录,但我无法从服务帐户访问共享文件。“你能帮帮我吗?”大卫·迪雷尔把这当作另一个问题问。如果服务帐户的上载文件确保您更改权限以授予自己访问权限,则服务帐户可能会很棘手。还可以尝试执行file.list添加q参数并查找与我共享的。谢谢,我已经了解如何解决这个问题。如果使用服务帐户,我建议您在这里使用,这样您就可以模拟自己并直接获取文件,而不必与服务帐户共享每个文件。非常感谢您!我设法使用一个服务帐户通过客户端PHP库访问我的驱动器,现在我面临另一个问题。。。如何列出或读取已从我的帐户与服务帐户共享的文件的某些属性?例如,我可以通过服务帐户将文件上载到共享目录,但我无法从服务帐户访问共享文件。“你能帮帮我吗?”大卫·迪雷尔把这当作另一个问题问。如果服务帐户的上载文件确保您更改权限以授予自己访问权限,则服务帐户可能会很棘手。还可以尝试执行file.list添加q参数并查找与我共享的。谢谢,我已经理解了如何处理这个问题。请参阅