我想将这个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进行授权
我的Guzzle 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;
}
}
感谢解决方案的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;
}
}