首页 > 解决方案 > JavaScript 等效于 Python 的 oauth2client POST 请求到 Google 提醒

问题描述

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

https://github.com/jonahar/google-reminders-cli

我已经在https://developers.google.com/identity/protocols/OAuth2UserAgent的帮助下移植了授权

我的 JavaScript 版本:https ://github.com/Jinjinov/google-reminders-js

现在我需要移植 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,
    )

我的 XMLHttpRequest POST 请求返回:

我的代码(带有授权和访问令牌的完整代码在 GitHub 上):

function encodeObject(params) {
    var query = [];
    for (let key in params) {
      let val = encodeURIComponent(key) + "=" + encodeURIComponent(params[key]);
      query.push(val);
    }
    return query.join('&');
}

function list_reminders(num_reminders, access_token, callback) {

    var body = {
        '5': 1,  // boolean field: 0 or 1. 0 doesn't work ¯\_(ツ)_/¯
        '6': num_reminders,  // number of reminders to retrieve
    };

    body['access_token'] = access_token;

    //body = JSON.stringify(body);
    body = encodeObject(body);

    var xhr = new XMLHttpRequest();

    xhr.open('POST', 'https://reminders-pa.clients6.google.com/v1internalOP/reminders/list' + '?' + body);
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

    //xhr.open('POST', 'https://reminders-pa.clients6.google.com/v1internalOP/reminders/list');
    //xhr.setRequestHeader('Content-type', 'application/json');

    xhr.onreadystatechange = function (e) {
        if (xhr.readyState === 4 && xhr.status === 200) {
            var content_dict = JSON.parse(xhr.response);

            if (!('1' in content_dict)) {
                console.log('No reminders found');
            }
            else {
                var reminders_dict_list = content_dict['1'];
                var reminders = [];

                for(var reminder_dict of reminders_dict_list) {
                    reminders.push(build_reminder(reminder_dict));
                }

                callback(reminders);
            }
        }
        else if (xhr.readyState === 4 && xhr.status === 401) {
            callback(null);
        }
    }

    //xhr.send(body);
    xhr.send(null);
}

标签: javascriptpythonpostgoogle-oauth

解决方案


我试图以相同的方式发送正文和访问令牌。

解决方案是将访问令牌作为 url 编码发送,将正文作为 json 发送:

function list_reminders(num_reminders, access_token, callback) {
    /*
    returns a list of the last num_reminders created reminders, or
    None if an error occurred
    */

    var xhr = new XMLHttpRequest();

    xhr.open('POST', 'https://reminders-pa.clients6.google.com/v1internalOP/reminders/list' + '?' + 'access_token=' + access_token);
    xhr.setRequestHeader('Content-type', 'application/json+protobuf');

    xhr.onreadystatechange = function (e) {
        if (xhr.readyState === 4 && xhr.status === 200) {
            var content_dict = JSON.parse(xhr.response);

            if (!('1' in content_dict)) {
                console.log('No reminders found');
            }
            else {
                var reminders_dict_list = content_dict['1'];
                var reminders = [];

                for(var reminder_dict of reminders_dict_list) {
                    reminders.push(build_reminder(reminder_dict));
                }

                callback(reminders);
            }
        }
        else if (xhr.readyState === 4 && xhr.status === 401) {
            callback(null);
        }
    }

    var body = {
        '5': 1,  // boolean field: 0 or 1. 0 doesn't work ¯\_(ツ)_/¯
        '6': num_reminders,  // number of reminders to retrieve
    };

    xhr.send(JSON.stringify(body));
}

推荐阅读