была задача прикуртить на сайт работу с гугли api drive, что бы можно было шарить нужные папки, сделал, но возникла проблема, что расшарить мог только владелец папок. После чтения доков вроде как понял что если делать авторизацию не через Create web application (client ID,secret key,redirectURI) а создать Google services account, получить ключ. то сможет шарить тогда папки любой пользователь. В экземплах Php google drive нашел пример авторизаии через гугл сервис акаунт(данные вставлял - всё работает)
PHP |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
| <?php
session_start();
include_once "templates/base.php";
/************************************************
Make an API request authenticated with a service
account.
************************************************/
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Books.php';
/************************************************
ATTENTION: Fill in these values! You can get
them by creating a new Service Account in the
API console. Be sure to store the key file
somewhere you can get to it - though in real
operations you'd want to make sure it wasn't
accessible from the webserver!
The name is the email address value provided
as part of the service account (not your
address!)
Make sure the Books API is enabled on this
account as well, or the call will fail.
************************************************/
$client_id = '<YOUR_CLIENT_ID>';
$service_account_name = '';
$key_file_location = '';
echo pageHeader("Service Account Access");
if ($client_id == '<YOUR_CLIENT_ID>'
|| !strlen($service_account_name)
|| !strlen($key_file_location)) {
echo missingServiceAccountDetailsWarning();
}
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$service = new Google_Service_Books($client);
/************************************************
If we have an access token, we can carry on.
Otherwise, we'll get one with the help of an
assertion credential. In other examples the list
of scopes was managed by the Client, but here
we have to list them manually. We also supply
the service account
************************************************/
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/books'),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
/************************************************
We're just going to make the same call as in the
simple query as an example.
************************************************/
$optParams = array('filter' => 'free-ebooks');
$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
echo "<h3>Results Of Call:</h3>";
foreach ($results as $item) {
echo $item['volumeInfo']['title'], "<br /> \n";
} |
|
перписал его потом расширвания прав с минимальными изменениями
PHP |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| <?php
session_start();
include_once "templates/base.php";
/************************************************
Make an API request authenticated with a service
account.
************************************************/
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';
$client_id = '1077677978854-deaidfbbb36erpstbjsvqh6s1iftg36k.apps.googleusercontent.com';
$service_account_name = '1077677978854-deaidfbbb36erpstbjsvqh6s1iftg36k@developer.gserviceaccount.com';
$key_file_location = './8c320ff616a77aec2421642de64449dad6f4a9ca-privatekey.p12';
$client = new Google_Client();
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/drive'),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
$service = new Google_Service_Drive($client);
$newPermission= new Google_Service_Drive_Permission();
$newPermission->setType('user');
$newPermission->setRole('reader');
$newPermission->setValue('rizr19911006@gmail.com');
try {
return $service->permissions->insert('1PXwPONvgN6yB2p6HbC98XiJsxBwPZ2WIM3QmeXjWLlo', $newPermission);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
} |
|
авторизация вроде как проходит но ошибка An error occurred: Error calling POST
https://www.googleapis.com/dri... rmissions: (500) Internal Error
что может быть? или гугл сэрвис акаунт не позволяет такое делать в отличии от обычной авторизации?