Javascript 如何使用HTTP POST请求从chrome扩展插入新事件,使用google日历api?
由于CORS,我无法使用js库,因此我的替代方法是使用fetch创建http请求。当我尝试使用新事件的数据发送POST请求时,控制台向我抛出以下错误: 加载资源失败:服务器响应状态为400() 这是我的代码:Javascript 如何使用HTTP POST请求从chrome扩展插入新事件,使用google日历api?,javascript,jquery,google-chrome-extension,request,google-calendar-api,Javascript,Jquery,Google Chrome Extension,Request,Google Calendar Api,由于CORS,我无法使用js库,因此我的替代方法是使用fetch创建http请求。当我尝试使用新事件的数据发送POST请求时,控制台向我抛出以下错误: 加载资源失败:服务器响应状态为400() 这是我的代码: var evento = { 'summary': 'Test', 'location': 'A random place', 'description': 'A random description', 'start': { 'dateTime': '2021-0
var evento = {
'summary': 'Test',
'location': 'A random place',
'description': 'A random description',
'start': {
'dateTime': '2021-04-30T09:00:00-07:00Z',
'timeZone': 'America/Mexico_City'
},
'end': {
'dateTime': '2021-04-30T17:00:00-07:00Z',
'timeZone': 'America/Mexico_City'
},
'recurrence': [
'RRULE:FREQ=DAILY;COUNT=2'
],
'attendees': [
{ 'email': 'test1@gmail.com' },
{ 'email': 'test213@gmail.com' }
],
'reminders': {
'useDefault': false,
'overrides': [
{ 'method': 'email', 'minutes': 24 * 60 },
{ 'method': 'popup', 'minutes': 10 }
]
}
};
chrome.identity.getAuthToken({ 'interactive': true }, function(token) {
console.log(token)
var init = {
'method' : 'POST',
'async' : true,
'headers': {
'Authorization' : 'Bearer ' + token,
'Content-Type': 'application/json'
},
body: JSON.stringify(evento)
};
fetch('https://www.googleapis.com/calendar/v3/calendars/primary/events', init)
.then((response) => response.json()) // Transform the data into json
.then(function(data) {
console.log(data);
})
})
这是manifest.json:
{
"manifest_version": 2,
"name": "Coffee Test",
"description": "Another attempt to make this thing work!",
"version": "1.0",
"icons": { "128": "128-icon.png" },
"key": "YOUR_CHROME_KEY",
"content_scripts": [
{
"matches": ["*://*.random.net/*"],
"exclude_matches": [
"*://*.random.net/*/results/*"
],
"js": ["background.js", "jquery-3.3.1.min.js"],
"run_at": "document_end"
}
],
"background": {
"scripts": [
"jquery-3.3.1.min.js",
"background.js"],
"persistent": false
},
"browser_action": {
"default_popup": "popup.html",
"default_title": "Extensión Test1"
},
"content_security_policy": "script-src 'self' 'unsafe-eval' https://apis.google.com; object-src 'self'",
"permissions": ["tabs", "identity", "https://apis.google.com/", "*://*.random.net/*"],
"oauth2": {
"client_id": "YOUR_CLIENT_CODE",
"scopes": [
"https://www.googleapis.com/auth/calendar"
]
}
}
我对GET没有问题,只对POST请求有问题
我已经查看了官方文档,但我仍然不清楚我的请求中的错误是什么在点击你问题中的链接后,我从谷歌得到了404-“请求的URL[…]未找到”。根据你得到的回复(),你的请求似乎无效。提到您还需要
https://www.googleapis.com/auth/calendar.events
使用此端点的范围。并添加https://www.googleapis.com/
到权限
。
{
"manifest_version": 2,
"name": "Coffee Test",
"description": "Another attempt to make this thing work!",
"version": "1.0",
"icons": { "128": "128-icon.png" },
"key": "YOUR_CHROME_KEY",
"content_scripts": [
{
"matches": ["*://*.random.net/*"],
"exclude_matches": [
"*://*.random.net/*/results/*"
],
"js": ["background.js", "jquery-3.3.1.min.js"],
"run_at": "document_end"
}
],
"background": {
"scripts": [
"jquery-3.3.1.min.js",
"background.js"],
"persistent": false
},
"browser_action": {
"default_popup": "popup.html",
"default_title": "Extensión Test1"
},
"content_security_policy": "script-src 'self' 'unsafe-eval' https://apis.google.com; object-src 'self'",
"permissions": ["tabs", "identity", "https://apis.google.com/", "*://*.random.net/*"],
"oauth2": {
"client_id": "YOUR_CLIENT_CODE",
"scopes": [
"https://www.googleapis.com/auth/calendar"
]
}
}