我想将这个用于Google提醒的开源Python库移植到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进行的
我的口吻客户端POST请求返回HTTP 400 --糟糕的请求--尽管Python版本工作正常。
我用:
我的代码(包含授权和$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;
}
}发布于 2019-12-15 19:28:46
感谢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;
}
}https://stackoverflow.com/questions/59303213
复制相似问题