Recover json data from youtube api call - javascript

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);
});

Related

bad request while using google index api in javascript

Hello evrey one i want to make a script that update my new urls of my website by using google indexing api from http://localhost:8000/test.html
i add http://localhost:8000 to Authorised JavaScript origins in the client ID
but i have error 400 :
Sign-in successful
cb=gapi.loaded_0:228 GET https://content.googleapis.com/discovery/v1/apis/indexing/v3/rest?pp=0&fields=kind%2Cname%2Cversion%2CrootUrl%2CservicePath%2Cresources%2Cparameters%2Cmethods%2CbatchPath%2Cid&key='my api' 400
wh # cb=gapi.loaded_0:228
g # cb=gapi.loaded_0:228
xh # cb=gapi.loaded_0:229
(anonymous) # cb=gapi.loaded_0:229
d # cb=gapi.loaded_0:186
b # cb=gapi.loaded_0:181
Error loading GAPI client for API
{error: {…}}
error:
code: 400
message: "Request contains an invalid argument."
status: "INVALID_ARGUMENT"
__proto__: Object
__proto__: Object
i change the browser from chrome to edge it work one time then i shutdown my pc but day after the problem apear again.
i am using the api expoler script : link
i put the script and the console log as image
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="https://apis.google.com/js/api.js"></script>
<script>
/**
* Sample JavaScript code for indexing.urlNotifications.publish
* 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/indexing"
})
.then(function() {
console.log("Sign-in successful");
},
function(err) {
console.error("Error signing in", err);
});
}
function loadClient() {
gapi.client.setApiKey("My-API");
return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/indexing/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.indexing.urlNotifications.publish({
"resource": {
"url": "example",
"type": "URL_UPDATED"
}
})
.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: "my OAuth client ID"
});
});
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>
</body>
</html>
here the console log
thanx to god, i try loadClient then authenticate and it works
like this <button onclick="loadClient().then(authenticate)">authorize and load</button>
then i use chrome incognito
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="https://apis.google.com/js/api.js"></script>
<script>
/**
* Sample JavaScript code for indexing.urlNotifications.publish
* 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/indexing"})
.then(function () { console.log("Sign-in successful"); },
function (err) { console.error("Error signing in", err); });
}
function loadClient() {
gapi.client.setApiKey("My API");
return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/indexing/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.indexing.urlNotifications.publish({
"resource": {
"url": "my web site",
"type": "URL_UPDATED"
}
})
.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:"My client ID"});
});
</script>
<button onclick="loadClient().then(authenticate)">authorize and load</button>
<button onclick="execute()">execute</button>
</body>
</html>
GAPI client loaded for API
Sign-in successful
Response {result: {…}, body: "{↵ "urlNotificationMetadata": {↵ "url": "https…e": "2020-09-15T20:34:07.091671446Z"↵ }↵ }↵}↵", headers: {…}, status: 200, statusText: null}
<script src="https://apis.google.com/js/api.js"></script>
<script>
/**
* Sample JavaScript code for indexing.urlNotifications.publish
* See instructions for running APIs Explorer code samples locally:
* https://developers.google.com/explorer-help/code-samples#javascript
*/
function authenticate() {
return gapi.auth2.getAuthInstance()
.signIn({scope: "https://www.googleapis.com/auth/indexing"})
.then(function() { console.log("Sign-in successful"); },
function(err) { console.error("Error signing in", err); });
}
function loadClient() {
gapi.client.setApiKey("xxxxxxxxxxxxxxxxxxxx");
return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/indexing/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.indexing.urlNotifications.publish({
"resource": {}
})
.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: "samplexxxxxxxxxxxxxxxxxx.apps.googleusercontent.com"});
});
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>
not working and
<script src="https://apis.google.com/js/api.js"></script>
<script>
/**
* Sample JavaScript code for indexing.urlNotifications.publish
* See instructions for running APIs Explorer code samples locally:
* https://developers.google.com/explorer-help/code-samples#javascript
*/
function authenticate() {
return gapi.auth2.getAuthInstance()
.signIn({scope: "https://www.googleapis.com/auth/indexing"})
.then(function() { console.log("Sign-in successful"); },
function(err) { console.error("Error signing in", err); });
}
function loadClient() {
gapi.client.setApiKey("xxxxxxxxxxxxxxxxxxxx");
return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/indexing/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.indexing.urlNotifications.publish({
"resource": {}
})
.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: "samplexxxxxxxxxxxxxxxxxx.apps.googleusercontent.com"});
});
</script>
<button onclick="loadClient().then(authenticate)">authorize and load</button>
<button onclick="execute()">execute</button>
not working all error;
code code: 400
message: "Request contains an invalid argument."
status: "INVALID_ARGUMENT"

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>

How can I send messages to my queue and a post request?

I have a function that posts data to the database. It works fine but I would also like to use the same function to send a message to trigger another function.
I have tried to simultaneouslysend the message and make the post request but at this moment only the post request works
Here is what my code looks like
const params = {
"TableName": "sites",
"Item": {
userId: event.requestContext.identity.cognitoIdentityId,
siteId: siteIdFinal,
...data,
createdAt: Date.now()
}
};
const messageParams = {
MessageBody: 'Waddup fam',
QueueUrl: ' https://sqs.eu-west-1.amazonaws.com/106845550704/MyQueue'
};
try {
await dynamoDbLib.call("put", params);
sqs.sendMessage(messageParams, (err, data) => {
if (err) {
console.log("Error: "+err);
} else {
console.log("Success: "+data.MessageId);
}
});
return success(params.Item);
} catch (e) {
console.log(e);
return failure({ status: false });
}
I am not getting any error I am just getting returned back the data that has been posted. I thought i should receive the message Id of the message I sent but I am not getting that. When ever I look at the cloudwatch logs, the message isnt sent
Your async function returns params.Item before sendMessage executes a callback.
Use a promise to make sure that both methods finish properly
await sqs.sendMessage(messageParams).promise()
.then(function(data) {
console.log("Success: "+data.MessageId);
}).catch(function(err) {
console.log("Error: "+err);
});
More on aws-sdk and promises:
https://aws.amazon.com/blogs/developer/support-for-promises-in-the-sdk/

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

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

Get a JSON response from an API?

I'm posting to an api where a failed response is output on purpose:
return response()->json([
'message' => 'Record not found',
], 422);
In Chrome dev tools I can see I get a 422 response with the response of {"message":"Record not found"}
In javascript I wish to get the message and log it, but i'm struggling to do so, here's my javascript:
axios.post('/api/test', {
name: 'test'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error) //this is Error: Request failed with status code 422
console.log(error.message); //this is blank
});
How can I get the message?
I found a code that can help you understand the catch block very well here :
axios.post('/api/test', {
name: 'test'
})
.then((response) => {
// Success
})
.catch((error) => {
// Error
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
// console.log(error.response.data);
// console.log(error.response.status);
// console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.log(error.config);
});
try to catch like this:
.catch(function(error){
console.log(error);
if (error.response) {
console.log(error.response.data.message);
}
});

Categories

Resources