gpt4 book ai didi

JavaScript 相当于 Python 的 oauth2client 对 Google 提醒的 POST 请求

转载 作者:太空宇宙 更新时间:2023-11-03 20:00:06 25 4
gpt4 key购买 nike

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

  • HTTP 400 - 错误请求 - 如果我使用“application/x-www-form-urlencoded”
  • HTTP 401 - 未经授权 - 如果我使用“application/json”

我的代码(带有授权和访问 token 的完整代码位于 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);
}

最佳答案

我尝试以相同的方式发送正文和访问 token 。

解决方案是将访问 token 作为 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));
}

关于JavaScript 相当于 Python 的 oauth2client 对 Google 提醒的 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59302894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com