构建Chrome扩展程序,尝试通过users.labels:create方法创建新标签。我成功检索了访问权限,我认为权限设置得当,但我仍然得到400,否则我无法找到关于这个问题的其他内容,尽管我怀疑我错过了一些非常明显的内容。
(如果我的代码没有放弃,这是我的第一个网络项目,所以任何帮助都是真诚的感谢,我很感激你的时间。)
var postRequestUrl = "https://www.googleapis.com/gmail/v1/users/me/labels?access_token=" + accessToken;
$.ajax({
url: postRequestUrl,
method: "POST",
contentType: "application/json",
data: {
name: "posting test label",
labelListVisibility: "labelShow",
messageListVisibility: "show"
}
}).done(function(msg){
alert("Success?")
})
{
"manifest_version": 2,
"key": "redacted>",
"name": "<redacted>",
"description": "Description",
"version": "0.0.2.0",
"default locale": "en",
"icons": { "128": "imgs/pledge_pin.png"},
"content_scripts" : [
{
"matches": ["*://mail.google.com/mail/*"],
"js": ["js/jquery.js", "js/compose.js", "bower_components/jqnotifybar/jquery.notifyBar.js"],
"css": ["css/stylesheet.css", "bower_components/jqnotifybar/css/jquery.notifyBar.css"]
}
],
"background": {
"scripts": ["scripts/background.js", "js/jquery.js"]
},
"permissions": [
"identity",
"https://www.googleapis.com/*"
],
"oauth2": {
"client_id": "<redacted>",
"scopes": ["https://www.googleapis.com/auth/gmail.modify", "https://www.googleapis.com/auth/gmail.labels"]
}
}
POST https://www.googleapis.com/gmail/v1/users/me/labels?access_token=<token redacted> 400 (Bad Request)
n.ajaxTransport.k.cors.a.crossDomain.send @ jquery.js:4
n.extend.ajax @ jquery.js:4
(anonymous function) @ background.js:106
propertyNames.forEach.target.(anonymous function) @ extensions::SafeBuiltins:19
EventImpl.dispatchToListener @ extensions::event_bindings:395
propertyNames.forEach.target.(anonymous function) @ extensions::SafeBuiltins:19
$Array.forEach.publicClass.(anonymous function) @ extensions::utils:94
EventImpl.dispatch_ @ extensions::event_bindings:379
EventImpl.dispatch @ extensions::event_bindings:401
propertyNames.forEach.target.(anonymous function) @ extensions::SafeBuiltins:19
$Array.forEach.publicClass.(anonymous function) @ extensions::utils:94
messageListener @ extensions::messaging:188
propertyNames.forEach.target.(anonymous function) @ extensions::SafeBuiltins:19
EventImpl.dispatchToListener @ extensions::event_bindings:395
propertyNames.forEach.target.(anonymous function) @ extensions::SafeBuiltins:19
$Array.forEach.publicClass.(anonymous function) @ extensions::utils:94
EventImpl.dispatch_ @ extensions::event_bindings:379
EventImpl.dispatch @ extensions::event_bindings:401
propertyNames.forEach.target.(anonymous function) @ extensions::SafeBuiltins:19
$Array.forEach.publicClass.(anonymous function) @ extensions::utils:94
dispatchOnMessage @ extensions::messaging:316
答案 0 :(得分:4)
您忘记提供请求有效内容的内容类型,在这种情况下是JSON。 application/json
会做到这一点。此外,请求有效负载中不需要userId: "me"
,您必须对数据进行字符串化。
$.ajax({
url: "https://www.googleapis.com/gmail/v1/users/me/labels?access_token={YOUR_API_KEY}",
method: "POST",
contentType: "application/json", // Content type has to be specified.
data: JSON.stringify({ // You have to stringify your data.
name: "Example", // userId: "me" is not needed.
labelListVisibility: "labelShow",
messageListVisibility: "show"
}),
success: function(msg){
alert(JSON.stringify(msg));
},
error: function(msg){
alert(JSON.stringify(msg));
}
});
答案 1 :(得分:3)
您的请求网址中存在拼写错误。更新如下:
var postRequestUrl = "https://www.googleapis.com/gmail/v1/users/me/labels?access_token=" + accessToken;