Ani*_*dhi 4 php google-api google-drive-api google-api-php-client service-accounts
我正在尝试使用服务帐户将文件上传到我的Google云端硬盘.当我部署此代码时,我不希望用户授权,我希望他们上传到我的帐户.我通过PHP使用它,下面是我到目前为止的地方.
此代码基于官方文档提供的示例.当我运行php脚本时,我没有错误,我print_r结果变量.它有多个网址,当我使用我创建的相同帐户访问它时,它说我需要请求权限.当我查看我的Google Developers控制台时,它说它收到了请求并成功运行了它.
但是,该文件不会显示在我的Google云端硬盘中.我不确定在服务帐户上查看这些文件的位置,以及如何将此文件存入我的Google云端硬盘.以下是我的代码.
我在某个地方错过了许可,或者我应该以某种方式将服务帐户连接到普通帐户?
DEFINE("TESTFILE", 'testfile-small.txt');
if (!file_exists(TESTFILE)) {
$fh = fopen(TESTFILE, 'w');
fseek($fh, 1024 * 1024);
fwrite($fh, "!", 1);
fclose($fh);
}
// The setup
// Authentication Credentials
$client_id = '<my client id>'; //Client ID
$service_account_name = '<my client email'; //Email Address
$key_file_location = '<path to key>'; //key.p12
// Create the client
$client = new Google_Client();
$client->setApplicationName("IEEE_File_Upload");
$service = new Google_Service_Drive($client);
// Check for token
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
// Get the key
$key = file_get_contents($key_file_location);
// Create the credentials
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/drive'),
$key
);
// Set the credentials
$client->setAssertionCredentials($cred);
// Something with tokens
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
// Actual file stuff
// Now lets try and send the metadata as well using multipart!
$file = new Google_Service_Drive_DriveFile();
$file->setTitle("Hello World!");
$result = $service->files->insert(
$file,
array(
'data' => file_get_contents(TESTFILE),
'mimeType' => 'application/octet-stream',
'uploadType' => 'multipart'
)
);
echo "<h3>Results Of Call:</h3>";
foreach ($result as $item) {
echo $item['volumeInfo']['title'], "<br /> \n";
Run Code Online (Sandbox Code Playgroud)
DaI*_*mTo 10
服务帐户不是您,服务帐户是其自己的sudo用户实体类型的东西.服务帐户拥有自己的驱动器帐户,自己的Google日历..当您预先上传到服务帐户时,文件会上传到其Google驱动器帐户而不是您的帐户.
我建议您使用相同的脚本并执行文件列表以查看该文件是否已上载到服务帐户驱动器帐户.
没有服务帐户的Web界面,因此您无法登录并通过Web进行检查,所有这些都必须通过您的服务帐户代码完成.
归档时间: |
|
查看次数: |
3215 次 |
最近记录: |