Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP与Python的等价物';s oauth2client向谷歌提醒发布请求_Php_Python_Post_Guzzle_Google Oauth - Fatal编程技术网

PHP与Python的等价物';s oauth2client向谷歌提醒发布请求

PHP与Python的等价物';s oauth2client向谷歌提醒发布请求,php,python,post,guzzle,google-oauth,Php,Python,Post,Guzzle,Google Oauth,我想将这个开源Python库的Google提醒移植到PHP: 我在他的帮助下移植了授权书 我的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 = { 'c

我想将这个开源Python库的Google提醒移植到PHP:

我在他的帮助下移植了授权书

我的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,
    )
授权是由

我的Guzzle客户机POST请求返回HTTP 400-错误的请求-即使Python版本工作正常

我用过:

我的代码(GitHub上有授权的完整代码和$httpClient):


感谢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;
    }
}

您的内容类型标题似乎有所不同,而且我也怀疑使用
$body=(object)[…]
是否能以正确的格式发送数据。您可能应该以字符串形式提供正文内容,无论API实际需要何种格式。@04FS抱歉,因为我是在复制粘贴代码,所以删除了太多内容-我编辑了question@04FS因为还有其他几个bug,更改内容类型标题似乎没有任何区别,但最终是一个关键的修复,因此,如果你愿意,50分奖金是你的-谢谢!
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;
    }
}