Как правильно создать файлы разрешения на Google Drive REST API с помощью curl и phpPhp

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Как правильно создать файлы разрешения на Google Drive REST API с помощью curl и php

Сообщение Anonymous »

Я пытаюсь создать разрешение на диск Google в файле через Curl в PHP, но Google Drive API, похоже, выбрасывает ошибки.POST https://www.googleapis.com/drive/v3/fil ... ermissions

{
"requests": [
{
"type": "user",
"role": "commenter",
"emailAddress": "nancyxxxx@gmail.com"
}
]
}

Я попробовал два примера кода ниже
ниже мой пример кода 1.
$access_token = 'xxxxxxxxxxxx';
$file_id ='file id goes here';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/drive/v3/fil ... ermissions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $access_token",
'Content-Type: application/x-www-form-urlencoded'
]);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n \"requests\": [\n {\n \"type\": \"anyone\",\n \"role\": \"reader\"\n\n }\n ]\n}");
//curl_setopt($ch, CURLOPT_POSTFIELDS, '{\n "requests": [\n {\n "type": "anyone",\n "role": "reader"\n\n }\n ]\n}');
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n \"requests\": [\n {\n \"type\": \"anyone\",\n \"role\": \"reader\"\n\n }\n ]\n}");

$response = curl_exec($ch);

curl_close($ch);

var_dump($response);

ниже мой пример кода 2.
$access_token = 'xxxxxxxxxxxx';
$file_id ='file id goes here';
$headers = array(
"Authorization: Bearer $access_token",
"Content-Type: application/json"
);

$post_fields = '{
"requests": [
{
"type": "anyone",
"role": "reader"
}
]
}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/drive/v3/fil ... ermissions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
$response = curl_exec($ch);

var_dump($response);

Вот ошибка.{ "error": { "code": 400, "message": "The permission type field is required.", "errors": [ { "message": "The permission type field is required.", "domain": "global", "reason": "required", "location": "permission.type", "locationType": "other" } ] } } string(323) "{ "error": { "code": 400, "message": "The permission type field is required.", "errors": [ { "message": "The permission type field is required.", "domain": "global", "reason": "required", "location": "permission.type", "locationType": "other" } ] } } "
< /code>
Похоже, что данные запроса разрешения ниже не передаются в API или что API Google Drive Rest имеет некоторые ошибки. Как я могу продолжить? < /P>
{
"requests": [
{
"type": "anyone",
"role": "reader"

}
]
}


Подробнее здесь: https://stackoverflow.com/questions/786 ... g-curl-and
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»