[英]Create spreadsheet with Google Sheets API V4 API Key
I've figured out how to read values from a Budgeting Spreadsheet I created, but I can't figure out how to create a new spreadsheet with the Sheets API V4. 我已经弄清楚如何从我创建的预算电子表格中读取值,但是我不知道如何使用Sheets API V4创建新的电子表格。 I've been struggling with this problem for 5 months by now, has anyone solved this problem before? 到目前为止,我已经为这个问题苦苦挣扎了5个月,之前有人解决过这个问题吗?
Here's my code: 这是我的代码:
// READ - WORKING!
router.get("/get", (req, res) => {
var id = '1LoSF_4Z9aoiVvDsjFV9CMOd--vvz3fERfOPajVb2sv8';
var params = 'https://sheets.googleapis.com/v4/spreadsheets/?key='
var url = params + apiKey;
request(`https://sheets.googleapis.com/v4/spreadsheets/${id}?key=${apiKey}`, (error, response, body) => {
console.log("Body", body);
});
})
// Create - NOT WORKING!
router.post('/create', (req,res)=>{
request({
method: 'POST',
uri: `https://sheets.googleapis.com/v4/spreadsheets?fields=properties%2Ftitle&key=${apiKey}`
}, (error, response, body)=>{
console.log(body);
//Logs the body of the newly created spreadsheet
})
})
I used the guidelines from Google's API Explorer, you can find it here: 我使用了Google API Explorer中的指南,可以在这里找到它:
https://developers.google.com/apis-explorer/#p/sheets/v4/sheets.spreadsheets.create https://developers.google.com/apis-explorer/#p/sheets/v4/sheets.spreadsheets.create
Thank you! 谢谢!
How about this modification? 这个修改怎么样?
headers
using the access token. 使用访问令牌添加headers
。 body
for giving the title of created spreadsheet. 添加body
以提供创建的电子表格的标题。 request({
method: 'POST',
uri: 'https://sheets.googleapis.com/v4/spreadsheets?fields=properties%2Ftitle%2CspreadsheetId',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + accessToken,
},
body: JSON.stringify({properties: {title: "sampleTitle"}}),
}, (error, response, body) => {
console.log(body);
});
https://www.googleapis.com/auth/spreadsheets
to the scopes. 使用此脚本时,请在范围内使用访问令牌,包括https://www.googleapis.com/auth/spreadsheets
。 This scope is used to create the spreadsheet. 该范围用于创建电子表格。 I actually got it to work! 我实际上已经开始工作了! Similar to the previous response (thank you Tanaike!!!). 与先前的回复类似(感谢您Tanaike !!)。
request({
method: 'POST',
url: 'https://sheets.googleapis.com/v4/spreadsheets',
headers:{
'Authorization': 'Bearer (access token goes here)'
},
body: JSON.stringify({
properties: {
title: "Spreadsheet Title Goes Here"
}
})}, (error, response, body)=>{
if(!error && response.statusCode == 200){
var info = JSON.parse(body);
console.log(info);
} else {
console.log(error);
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.