PHP 相当于 Python 的 oauth2client POST 请求 Google 提醒
PHP equivalent of Python's oauth2client POST request to Google Reminders
我想将这个开源 Python 库移植到 Google 给 PHP 的提醒:
https://github.com/jonahar/google-reminders-cli
我已经在
https://developers.google.com/identity/protocols/OAuth2WebServer
我的PHP版本:https://github.com/Jinjinov/google-reminders-php
现在我需要移植 Python 的 oauth2client POST 请求:
body = {
'5': 1, # boolean field: 0 or 1. 0 doesn't work ¯\_(ツ)_/¯
'6': num_reminders, # number number of reminders to retrieve
}
HEADERS = {
'content-type': 'application/json+protobuf',
}
response, content = self.auth_http.request(
uri='https://reminders-pa.clients6.google.com/v1internalOP/reminders/list',
method='POST',
body=json.dumps(body),
headers=HEADERS,
)
通过 https://github.com/googleapis/google-api-php-client
进行授权
我的 Guzzle 客户端 POST 请求 returns HTTP 400 - 错误请求 - 尽管 Python 版本工作正常。
我用过:
- http://docs.guzzlephp.org/en/stable/request-options.html#headers
- http://docs.guzzlephp.org/en/stable/request-options.html#body
我的代码(具有授权的完整代码和 $httpClient 在 GitHub 上):
function list_reminders($httpClient, $num_reminders) {
$body = (object)[
'5' => 1, // boolean field: 0 or 1. 0 doesn't work ¯\_(ツ)_/¯
'6' => $num_reminders, // number of reminders to retrieve
];
$response = $httpClient->request(
'POST',
'https://reminders-pa.clients6.google.com/v1internalOP/reminders/list',
[
'headers' => [ 'content-type' => 'application/json' ],
'body' => json_encode($body)
]
);
if ($response->getStatusCode() == $HTTP_OK) {
$content = $response->getBody();
$content_dict = json_decode($content);
if (!array_key_exists('1', $content_dict)) {
return [];
}
$reminders_dict_list = $content_dict['1'];
$reminders = [];
foreach($reminders_dict_list as $reminder_dict) {
array_push($reminders, build_reminder($reminder_dict));
}
return $reminders;
}
else {
return null;
}
}
感谢04FS的解答('content-type'
应该是'application/json+protobuf'
)
如果其他人有兴趣:
function list_reminders($httpClient, $num_reminders) {
/*
returns a list of the last num_reminders created reminders, or
None if an error occurred
*/
$body = (object)[
'5' => 1, // boolean field: 0 or 1. 0 doesn't work ¯\_(ツ)_/¯
'6' => $num_reminders, // number of reminders to retrieve
];
$response = $httpClient->request(
'POST',
'https://reminders-pa.clients6.google.com/v1internalOP/reminders/list',
[
'headers' => [ 'content-type' => 'application/json+protobuf' ],
'body' => json_encode($body)
]
);
if ($response->getStatusCode() == 200) {
$content = $response->getBody();
$content_dict = json_decode($content, true);
if (!array_key_exists('1', $content_dict)) {
return [];
}
$reminders_dict_list = $content_dict['1'];
$reminders = [];
foreach($reminders_dict_list as $reminder_dict) {
array_push($reminders, build_reminder($reminder_dict));
}
return $reminders;
}
else {
return null;
}
}
我想将这个开源 Python 库移植到 Google 给 PHP 的提醒:
https://github.com/jonahar/google-reminders-cli
我已经在 https://developers.google.com/identity/protocols/OAuth2WebServer
我的PHP版本:https://github.com/Jinjinov/google-reminders-php
现在我需要移植 Python 的 oauth2client POST 请求:
body = {
'5': 1, # boolean field: 0 or 1. 0 doesn't work ¯\_(ツ)_/¯
'6': num_reminders, # number number of reminders to retrieve
}
HEADERS = {
'content-type': 'application/json+protobuf',
}
response, content = self.auth_http.request(
uri='https://reminders-pa.clients6.google.com/v1internalOP/reminders/list',
method='POST',
body=json.dumps(body),
headers=HEADERS,
)
通过 https://github.com/googleapis/google-api-php-client
进行授权我的 Guzzle 客户端 POST 请求 returns HTTP 400 - 错误请求 - 尽管 Python 版本工作正常。
我用过:
- http://docs.guzzlephp.org/en/stable/request-options.html#headers
- http://docs.guzzlephp.org/en/stable/request-options.html#body
我的代码(具有授权的完整代码和 $httpClient 在 GitHub 上):
function list_reminders($httpClient, $num_reminders) {
$body = (object)[
'5' => 1, // boolean field: 0 or 1. 0 doesn't work ¯\_(ツ)_/¯
'6' => $num_reminders, // number of reminders to retrieve
];
$response = $httpClient->request(
'POST',
'https://reminders-pa.clients6.google.com/v1internalOP/reminders/list',
[
'headers' => [ 'content-type' => 'application/json' ],
'body' => json_encode($body)
]
);
if ($response->getStatusCode() == $HTTP_OK) {
$content = $response->getBody();
$content_dict = json_decode($content);
if (!array_key_exists('1', $content_dict)) {
return [];
}
$reminders_dict_list = $content_dict['1'];
$reminders = [];
foreach($reminders_dict_list as $reminder_dict) {
array_push($reminders, build_reminder($reminder_dict));
}
return $reminders;
}
else {
return null;
}
}
感谢04FS的解答('content-type'
应该是'application/json+protobuf'
)
如果其他人有兴趣:
function list_reminders($httpClient, $num_reminders) {
/*
returns a list of the last num_reminders created reminders, or
None if an error occurred
*/
$body = (object)[
'5' => 1, // boolean field: 0 or 1. 0 doesn't work ¯\_(ツ)_/¯
'6' => $num_reminders, // number of reminders to retrieve
];
$response = $httpClient->request(
'POST',
'https://reminders-pa.clients6.google.com/v1internalOP/reminders/list',
[
'headers' => [ 'content-type' => 'application/json+protobuf' ],
'body' => json_encode($body)
]
);
if ($response->getStatusCode() == 200) {
$content = $response->getBody();
$content_dict = json_decode($content, true);
if (!array_key_exists('1', $content_dict)) {
return [];
}
$reminders_dict_list = $content_dict['1'];
$reminders = [];
foreach($reminders_dict_list as $reminder_dict) {
array_push($reminders, build_reminder($reminder_dict));
}
return $reminders;
}
else {
return null;
}
}