admin管理员组文章数量:1345007
I've been successfully using this URL to pull data from Google Sheets in JSON:
/<SHEETS_ID>/1/public/full?alt=json
I want to get the JSON for a Google Docs document now. What would the URL be to do that?
I know I can use the GET API, but I'm trying to do this using simple AJAX and no OAuth (the file is public)
I've been successfully using this URL to pull data from Google Sheets in JSON:
https://spreadsheets.google./feeds/cells/<SHEETS_ID>/1/public/full?alt=json
I want to get the JSON for a Google Docs document now. What would the URL be to do that?
I know I can use the GET API, but I'm trying to do this using simple AJAX and no OAuth (the file is public)
Share Improve this question edited Jan 20, 2020 at 14:21 Rafa Guillermo 15.4k3 gold badges21 silver badges59 bronze badges asked Jan 19, 2020 at 12:33 Bill FersterBill Ferster 3471 gold badge5 silver badges17 bronze badges 1- Can I ask you about the values of Google Document you need? The text of the Document, the page styles and so on. Because Google Docs API is different from Google Sheets API, at first, I would like to confirm about it. – Tanaike Commented Jan 19, 2020 at 23:04
2 Answers
Reset to default 5Answer:
While the Google Spreadsheets Data API that hosts the https://spreadsheets.google./feeds/cells/<SHEETS_ID>/1/public/full?alt=json
endpoint is still live, the Google Docs one is not and obtaining a Doc in JSON format in this way is no longer possible.
More Information:
The endpoint you are referencing to obtain Sheets data as a JSON structure is part of the Google Data APIs and is one of Google's older APIs - most of which have been replaced with newer APIs. The Spreadsheets one, as you can see here, is still live, where there is an interactive example explaining how to form this URL.
As you can see in the GData API Directory documentation however, the Google Documents List Data API has been shut down and replaced by the Google Drive API. Unfortunately, this means that the method you are looking for has been deprecated and so using either the Drive or Docs APIs now need to be used.
References:
- Simple example of retrieving JSON feeds from Spreadsheets Data API
- Google Data APIs - GData API Directory
- Google Drive API
You can find that information in Google Docs API code samples here.
Specifically it is a GET request to https://docs.googleapis./v1/documents/{documentId}
.
If successful, the response body contains an instance of Document
which you can convert as JSON.
Example in Javascript:
<!DOCTYPE html>
<html>
<head>
<title>
Docs API Extract Body
</title>
<meta charset="utf-8"/>
</head>
<body>
<p>
Docs API Extract Body
</p>
<!--Add buttons to initiate auth sequence and sign out-->
<button id="authorize-button" style="display: none;">Authorize</button>
<button id="signout-button" style="display: none;">Sign Out</button>
<pre id="content"></pre>
<script type="text/javascript">
// Client ID and API key from the Developer Console
var CLIENT_ID = '<YOUR_CLIENT_ID>'
var API_KEY = '<YOUR_API_KEY>';
// Array of API discovery doc URLs for APIs used by the sample
var DISCOVERY_DOCS = [
'https://docs.googleapis./$discovery/rest?version=v1'];
// Authorization scopes required by the API; multiple scopes can be
// included, separated by spaces.
var SCOPES = "https://www.googleapis./auth/documents.readonly";
var authorizeButton = document.getElementById('authorize-button');
var signoutButton = document.getElementById('signout-button');
/**
* On load, called to load the auth2 library and API client library.
*/
function handleClientLoad() {
gapi.load('client:auth2', initClient);
}
/**
* Initializes the API client library and sets up sign-in state
* listeners.
*/
function initClient() {
gapi.client.init({
apiKey: API_KEY,
clientId: CLIENT_ID,
discoveryDocs: DISCOVERY_DOCS,
scope: SCOPES
}).then(function () {
// Listen for sign-in state changes.
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
// Handle the initial sign-in state.
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
authorizeButton.onclick = handleAuthClick;
signoutButton.onclick = handleSignoutClick;
});
}
/**
* Called when the signed in status changes, to update the UI
* appropriately. After a sign-in, the API is called.
*/
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
authorizeButton.style.display = 'none';
signoutButton.style.display = 'block';
printDocBody();
} else {
authorizeButton.style.display = 'block';
signoutButton.style.display = 'none';
}
}
/**
* Sign in the user upon button click.
*/
function handleAuthClick(event) {
gapi.auth2.getAuthInstance().signIn();
}
/**
* Sign out the user upon button click.
*/
function handleSignoutClick(event) {
gapi.auth2.getAuthInstance().signOut();
}
/**
* Append a pre element to the body containing the given message
* as its text node. Used to display the results of the API call.
*
* @param {string} message Text to be placed in pre element.
*/
function appendPre(message) {
var pre = document.getElementById('content');
var textContent = document.createTextNode(message + '\n');
pre.appendChild(textContent);
}
/**
* Prints the JSON body of a document.
*/
function printDocBody() {
gapi.client.docs.documents.get({
documentId: 'DOCUMENT_ID'
}).then(function(response) {
var doc = response.result;
appendPre(JSON.stringify(doc.body, null, 4));
},function(response) {
appendPre('Error: ' + response.result.error.message);
});
}
</script>
<script async="" defer="" onload="this.onload=function(){};handleClientLoad()" onreadystatechange="if (this.readyState === 'plete') this.onload()" src="https://apis.google./js/api.js"></script>
</body>
</html>
本文标签: javascriptUsing Google docs as a data endpoint to get JSONStack Overflow
版权声明:本文标题:javascript - Using Google docs as a data endpoint to get JSON - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743789816a2539357.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论