Is there a way to execute spreadsheets.values.update without OAuth authentication? - javascript

I'm making a discord bot that will be open to the public to use, and it will work by updating a google spreadsheet with values given in a command. Unfortunately, from what I see in the API documentation's "Try this API" feature, I can only use Google OAuth v2. Using an API key as a form of authentication gives me the error: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. How can I make it so that a user doesn't need to sign in for this to work?
This is the code:
function authenticate() {
return gapi.auth2.getAuthInstance()
.signIn({scope: "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/spreadsheets"})
.then(function() { console.log("Sign-in successful"); },
function(err) { console.error("Error signing in", err); });
}
function loadClient() {
return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/sheets/v4/rest")
.then(function() { console.log("GAPI client loaded for API"); },
function(err) { console.error("Error loading GAPI client for API", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function execute() {
return gapi.client.sheets.spreadsheets.values.update({
"spreadsheetId": "1AFCiplLlZRdO5Phb4DUh8M8sKKnUimJftVDGH9UAA3M",
"range": "Ratings!H2",
"valueInputOption": "RAW",
"resource": {
"values": [
[
7.5
]
]
}
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
gapi.load("client:auth2", function() {
gapi.auth2.init({client_id: YOUR_CLIENT_ID});
});
Thank you if you can help me, this has been stumping me for a while now.
spreadsheet: https://docs.google.com/spreadsheets/d/1AFCiplLlZRdO5Phb4DUh8M8sKKnUimJftVDGH9UAA3M/edit#gid=569426832

Related

Recover json data from youtube api call

I executed the code to retrieve the info from subscriber and all is well but the problem I can't get the info in json format
<script src="https://apis.google.com/js/api.js"></script>
<script>
/**
* Sample JavaScript code for youtube.subscriptions.list
* See instructions for running APIs Explorer code samples locally:
* https://developers.google.com/explorer-help/guides/code_samples#javascript
*/
function authenticate() {
return gapi.auth2.getAuthInstance()
.signIn({scope: "https://www.googleapis.com/auth/youtube.readonly"})
.then(function() { console.log("Sign-in successful"); },
function(err) { console.error("Error signing in", err); });
}
function loadClient() {
gapi.client.setApiKey("AIzxxxxxxxxxxx");
return gapi.client.load("https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest")
.then(function() { console.log("GAPI client loaded for API"); },
function(err) { console.error("Error loading GAPI client for API", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function execute() {
return gapi.client.youtube.subscriptions.list({
"part": [
"snippet,contentDetails"
],
"forChannelId": "UCASxxxxxxxxxxxx",
"mine": true
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
?????????????
},
function(err) { console.error("Execute error", err); });
}
gapi.load("client:auth2", function() {
gapi.auth2.init({client_id: "5000000020-au42cxxxxxxxxxxxx.apps.googleusercontent.com"});
});
</script>
I looked at the console and I saw that the information is retrieved in response but not in json format
enter image description here
Once you have the response of the API, you need to check its content and get the values you need.
Example:
.then(function(response) {
// This is the response of the API.
console.log(response);
// Once you got the response, view its content.
// For example, if you want get the "results" value
// of the "response", use:
console.log(response.results);
},
function(err) {
console.error("Execute error", err);
});

Google API demo does not work locally; GAPI is null

I copied Google's demo code from this tutorial:
https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get
However, when I tried to run this locally, I get the error below.
testoJS.html:14 Uncaught TypeError: Cannot read property 'signIn' of null
at authenticate
I'm not sure why the gapi variable is null even though the code imports the api module beforehand.
Here is a copy of my code; my client id, api key, and spreadsheetid are correct - I tested those values in the above website's "try this API" section. The http request works great there and returns the spreadsheet's content. However, I can't seem to run it locally.
<html>
<head></head>
<body>
<script src="https://apis.google.com/js/api.js"></script>
<script>
/**
* Sample JavaScript code for sheets.spreadsheets.values.get
* See instructions for running APIs Explorer code samples locally:
* https://developers.google.com/explorer-help/guides/code_samples#javascript
*/
function authenticate() {
return gapi.auth2.getAuthInstance()
.signIn({scope: "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/spreadsheets.readonly"})
.then(function() { console.log("Sign-in successful"); },
function(err) { console.error("Error signing in", err); });
}
function loadClient() {
gapi.client.setApiKey("API_KEY");
return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/sheets/v4/rest")
.then(function() { console.log("GAPI client loaded for API"); },
function(err) { console.error("Error loading GAPI client for API", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function execute() {
return gapi.client.sheets.spreadsheets.values.get({
"spreadsheetId": "SPREADSHEETID",
"range": "A1:D4"
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
gapi.load("client:auth2", function() {
gapi.auth2.init({client_id: "CLIENT_ID"});
});
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>
</body>
</html>

Shared files can't access(get list of shared file) using drive.file scope Google Drive API

The file is shared success and the shared user gets an email notification, file display in the user google drive but when we try using API to get shared files, it is not working.
var SCOPES = ["https://www.googleapis.com/auth/drive.file", "profile"];
function createPermissions(fileId, body) {
gapi.client.load("drive", "v3", function() {
gapi.client.drive.permissions
.create({
fileId: fileId,
resource: body
})
.then(function(res) {
//console.log(res);
Swal.fire("Success!", "File has been success shared!", "success");
// do something
})
.catch(function(err) {
//console.log(err);
Swal.fire({
icon: "error",
title: "Oops...",
text: "Something went wrong! Plese try agian later!!",
footer: ""
});
// do something
});
});
}
The above code is working fine, the file is successfully shared but when shared user login in-app user can't access shared files.
Anyone please suggest/help to fix the above issue?
Thanks
I would suggest you call the Drive API in this way:
// This is a good scope for testing
const SCOPES = ["https://www.googleapis.com/auth/drive"];
// This code takes into consideration that you already did all the OAuth2.0 proccess
// correctly to connect to the Drive API
module.exports.init = async function (){
// Create the Drive service
const drive = google.drive({version: 'v3', oauth2Client});
// Create permissions for an user
await createPermissions(drive, null, null);
}
// This function will create the permissions to share a file using Promises
function createPermissions(drive, fileId, body){
// These parameters are for test, past the values you want as arguments
fileId = fileId || "your-file-id";
body = body || {
"role": "writer",
"type": "user",
"emailAddress": "user#domain"
};
// Create the promise and return the value from the promise if you want to
return new Promise((resolve, reject) => {
try {
// Call the endpoint, if there are no errors you will pass the results to resolve
// to return them as the value from your promise
return drive.permissions.create({
fileId: fileId,
resource: body
},
(err, results) => {
if(err) reject(`Drive error: ${err.message}`);
resolve(results);
});
} catch (error) {
console.log(`There was a problem in the promise: ${error}`);
}
});
}
I tried it and the files are shared successfully to the user I wanted. Keep in mind to build your body as:
{
"role": "writer",
"type": "user",
"emailAddress": "user#domain"
};
Docs
Here are some links to know more about the Drive API Permissions:
Permissions.
Permissions: create.

web javascript Google Play API OAuth 2.0 access

Hello I have a problem with data loading from Google Play API. I tried to load leaderboard data from my game (Looney Invaders). I want show game statistic at my custom website. I tried to do this with "Google API Client Library for JavaScript" for "authorized" OAuth 2.0 access.
I used sample example of "authorized" access to a Google API (using OAuth 2.0).
Javascript code:
<script type="text/javascript">
function handleClientLoad() {
gapi.load('client:auth2', initClient);
}
function initClient() {
gapi.client.init({
//API key from google
apiKey: '***',
discoveryDocs: ["www.googleapis.com/discovery/v1/apis/games/v1/rest"],
//ClientID (for OAuth 2.0) from google develope console
clientId: '***',
scope: 'www.googleapis.com/auth/games'
}).then(function (data) {
// Listen for sign-in state changes.
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
// Handle the initial sign-in state.
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
}, function (err) {
console.error('Our Error: ');
console.error(err);
});
}
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
makeApiCall();
}
}
function handleSignInClick(event) {
gapi.auth2.getAuthInstance().signIn();
}
function handleSignOutClick(event) {
gapi.auth2.getAuthInstance().signOut();
}
function makeApiCall() {
// Make an API call to the People API, and print the user's given name.
console.log('api call');
return gapi.client.games.leaderboards.list()
.then(function(response) {
console.log(response);
}, function(reason) {
console.log(reason);
});
}
</script>
<script async defer src="https://apis.google.com/js/api.js"
onload="this.onload=function(){};handleClientLoad()"
onreadystatechange="if (this.readyState === 'complete') this.onload()">
</script>
I test it locally at http://localhost, so I added "Authorized JavaScript origins" as localhost and "Authorized redirect URIs" as localhost/.
I have the following error result after API call (gapi.client.games.leaderboards.list()):
{
"error": {
"errors": [
{
"domain": "global",
"reason": "UserRegistrationIncomplete",
"message": "User has not completed registration."
}
],
"code": 401,
"message": "User has not completed registration."
}
}
Also initialization phase (gapi.client.init()) ends without any errors.
Please, help me to solve this problem, or tell what I do wrong?

Can't get Facebook Login to work on Meteor

I cannot get Meteor.loginWithFacebook to work, I have looked at so many Q&A's here, and they all do it differently but none works for me. (It has only worked with accounts-ui, but I need my custom login page)
I have installed accounts-password, accounts-facebook and service-configuration
Server Code (Not Real ClientId or Secret):
ServiceConfiguration.configurations.upsert({
service: "facebook"
}, {
$set: {
clientId: "2234324234324",
loginStyle: "redirect",
secret: "324234234234324234234"
}
});
Client Code (on click button, using ui-router controller as variable "vm"):
vm.facebook = function() {
Meteor.loginWithFacebook({
requestPermissions: ['email']
},
function(err) {
if (err) {
return console.log("There was an error : " + err);
} else {
console.log('No Error!');
}
});
};

Categories

Resources