首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >相当于Python对谷歌提醒的oauth2client帖子请求

相当于Python对谷歌提醒的oauth2client帖子请求
EN

Stack Overflow用户
提问于 2019-12-12 11:07:40
回答 1查看 225关注 0票数 2

我想将这个用于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请求:

代码语言:javascript
复制
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版本工作正常。

我用:

  • http://docs.guzzlephp.org/en/stable/request-options.html#headers
  • http://docs.guzzlephp.org/en/stable/request-options.html#body

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

代码语言:javascript
复制
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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-15 19:28:46

感谢04FS的解决方案('content-type'应该是'application/json+protobuf')

如果其他人对此感兴趣:

代码语言:javascript
复制
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;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59303213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档