Gau*_*hra 1 php web-services google-api google-drive-api
我是Google Drive API的新手。我正在将文件从服务器上传到Google Drive。
在我的Gmail帐户中
添加了一个与Google通信的Controller,具体如下:
set_include_path(get_include_path() . PATH_SEPARATOR . ROOT .DS. "vendors");
require_once('Google/Client/Google_Client.php');
require_once('Googl
Run Code Online (Sandbox Code Playgroud)e / Client / contrib / Google_DriveService.php');
define('CLIENT_ID','my_client_here....');
define('SERVICE_ACCOUNT_NAME','my_account_name here');
define('KEY_FILE',VENDORS.'Google/Client/key/privatekey.p12'); // what i was availed by google.
if(is_file(KEY_FILE)){
$client = new Google_Client();
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/drive'),
$key)
);
$service = new Google_DriveService($client);
$file = new Google_DriveFile();
$file->setTitle('Any sample file title');
$file->setDescription('An experiment to upload text file via server to server');
$file->setMimeType('text/plain');
$data = "Lorem ipsum is a dummy text";
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => 'text/plain',
));
print_r($createdFile);
Run Code Online (Sandbox Code Playgroud)
现在,此输出如下:
[kind] => drive#file
[id] => 0B-NwgyCI2GQENkswOHVNcVl2aDQ
[etag] => "11QSOvUX6KZKZVB6nCB3GloLTJQ/MTM5MDU2MzUwNTU2OQ"
[selfLink] => https://www.googleapis.com/drive/v2/files/0B-NwgyCI2GQENkswOHVNcVl2aDQ
[webContentLink] => https://docs.google.com/uc?id=0B-NwgyCI2GQENkswOHVNcVl2aDQ&export=download
[alternateLink] => https://docs.google.com/file/d/0B-NwgyCI2GQENkswOHVNcVl2aDQ/edit?usp=drivesdk
[iconLink] => https://ssl.gstatic.com/docs/doclist/images/icon_10_text_list.png
[title] => Any sample file title
[mimeType] => text/plain
[description] => An experiment to upload text file via server to server
[labels] => Array
(
[starred] =>
[hidden] =>
[trashed] =>
[restricted] =>
[viewed] => 1
)
[createdDate] => 2014-01-24T11:38:25.679Z
[modifiedDate] => 2014-01-24T11:38:25.569Z
[modifiedByMeDate] => 2014-01-24T11:38:25.569Z
[lastViewedByMeDate] => 2014-01-24T11:38:25.569Z
[parents] => Array
(
[0] => Array
(
[kind] => drive#parentReference
[id] => 0AONwgyCI2GQEUk9PVA
[selfLink] => https://www.googleapis.com/drive/v2/files/0B-NwgyCI2GQENkswOHVNcVl2aDQ/parents/0AONwgyCI2GQEUk9PVA
[parentLink] => https://www.googleapis.com/drive/v2/files/0AONwgyCI2GQEUk9PVA
[isRoot] => 1
)
)
[downloadUrl] => https://doc-04-60-docs.googleusercontent.com/docs/securesc/lptjog45u94o44tbsu1586vcuinvv74h/mlvse3i90c59eb4q6ggg7d4c6v6q4hfd/1390557600000/03476129820556261866/03476129820556261866/0B-NwgyCI2GQENkswOHVNcVl2aDQ?h=16653014193614665626&e=download&gd=true
[userPermission] => Array
(
[kind] => drive#permission
[etag] => "11QSOvUX6KZKZVB6nCB3GloLTJQ/-HVXFUAxHplEpHWK6MSWEcIX7d8"
[id] => me
[selfLink] => https://www.googleapis.com/drive/v2/files/0B-NwgyCI2GQENkswOHVNcVl2aDQ/permissions/me
[role] => owner
[type] => user
)
[originalFilename] => Any sample file title
[fileExtension] =>
[md5Checksum] => 2c06987d6ae9cfcd7060c68f4356231a
[fileSize] => 31
[quotaBytesUsed] => 31
[ownerNames] => Array
(
[0] => sdsdsdswrwew@developer.myemail.com
)
[owners] => Array
(
[0] => Array
(
[kind] => drive#user
[displayName] => dsdadasd3@developer.myemail.com
[isAuthenticatedUser] => 1
[permissionId] => 03476129820556261866
)
)
[lastModifyingUserName] => 89989989889@developer.myemailid.com
[lastModifyingUser] => Array
(
[kind] => drive#user
[displayName] => my_display_name
[isAuthenticatedUser] => 1
[permissionId] => 03476129820556261866
)
[editable] => 1
[copyable] => 1
[writersCanShare] => 1
[shared] =>
[appDataContents] =>
Run Code Online (Sandbox Code Playgroud)
..............
现在的主要问题是,通过此文件上传的文件未在我的Google驱动器文件列表中列出,但是如果我查询帐户中的可用文件,则只会得到通过此api上传的文件列表,而不是我在api中手动创建的文件列表谷歌驱动器界面。
See my code to get file list..
.....................
$client = new Google_Client();
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/drive'),
$key)
);
$service = new Google_DriveService($client);
....................
$files = $service->files->listFiles();
Run Code Online (Sandbox Code Playgroud)
Now Questions:
Please let me know where I am doing wrong, also let me know if what I am trying to achieve is possible or not. I have created a Service Account rather than Client ID for web application because the script will run automatically without any user interface.
Please suggest....
Why do the files not listed in my google drive list?
Because they don't belong to you. They belong to the service account.
Why the service account I created in google console is treated as like a different user.
Because that is what a service account, ie. it's own user.
如果您希望上传的文件包含在自己的云端硬盘内容中,则需要使用自己的帐户凭据访问云端硬盘SDK。这不需要涉及用户交互。您只需要一次性获取刷新令牌,然后使用该令牌来生成Drive的访问令牌。
| 归档时间: |
|
| 查看次数: |
2039 次 |
| 最近记录: |