GitHub API (Octokat.js): Cannot get Projects - javascript

I'm trying to access projects of a repository through GitHub API using Octokat.js.
The API guide says I need to use preview header, which I am using:
https://developer.github.com/v3/projects/
I'm getting error for property fetchAll of undefined (= projects).
I'm not sure what I'm doing wrong here - does anybody have an experience with this?
var Octokat = require('octokat')
var octo = new Octokat({
// put your own token here
token: 'XXX',
acceptHeader: 'application/vnd.github.inertia-preview+json'
})
octo.repos('YYY', 'ZZZ').projects.fetchAll((e, val) => {
this.projectsList = val
})

OK, turns out the API preview isn't implemented in the library yet.
https://github.com/philschatz/octokat.js/issues/144

Related

stripe.subscriptions.retrieve not working - Stripe Nodejs

When trying to retrieve a Stripe subscription I get
Logged Error: Cannot read properties of undefined (reading 'retrieve')
Here's how I'm trying to retrieve a subscription:
const stripeSubscription = await stripe.subscriptions.retrieve(
subId
);
console.log(stripeSubscription);
Reference: Retrieve subscription Stripe docs
I was using version:
"stripe": "^8.222.0"
Then upgraded to version
"stripe": "^10.10.0"
Still not working. What am I missing or doing anything wrong?
Any help or guide is appreciated. Thanks.
It looks like you're not fully initializing the client as it looks like a secret key is not being provided to it.
Where you currently have:
const stripe = require("stripe")
can you try updating that line to include your secret key as shown in line 1 of the snippet within the documentation that you referenced, so that it looks like this, but with your actual secret key:
const stripe = require("stripe")("sk_test_XXX")

WP REST API ERROR 401 While trying to fetch image from wp:featuredmedia

I'm trying to fetch featured image from my published post but it seems impossible! I get
an error :( that's the code:
function fetchSlideShow(){
let endpoint = "http://loreleiheckmann.com/wordpress/wordpress/wp-json/wp/v2/Vinyls?_embed";
fetch(endpoint)
.then(e => e.json())
.then(showSlideShow);
}
function showSlideShow(data){
console.log(data);
data.forEach(showSingleSlide);
showSlides();
}
function showSingleSlide(aSlide) {
let template = document.querySelector(".slide_template").content;
let clone = template.cloneNode(true);
console.log(aSlide);
clone.querySelector("img").setAttribute("src", aSlide._embedded["wp:featuredmedia"]
[0].media_details.source_url);
let SlideList = document.querySelector("#SlideList");
SlideList.appendChild(clone);
}
While going to the array I see error 401 :( and moreover: Cannot read property 'source_url' of undefined" I don't know what I'm doing wrong .. any insights?
HERE ARE THE ERRORS -> 401 ON CONSOLE + PROBLEM WITH URL.:
Please try changing your url in the endpoint variable:
let endpoint = "http://loreleiheckmann.com/wordpress/wordpress/wp-json/wp/v2/posts?per_page=20&_embed=wp:featuredmedia
If you need more data you can add them with a comma:
wp-json/wp/v2/posts?per_page=20&_embed=wp:term,wp:featuredmedia
Post per page is optional, but I would prefer having that set.
And you should write what image size you need. That way rest api will give you the right source url:
_embedded['wp:featuredmedia']['0'].media_details.sizes.medium_large.source_url
I think it would also be better practice if you use an ssl certificate, there are free by "Let's encrypt".
EDIT:
Your screenshot shows the rest api message:
wp:featuredmedia: Array(1)
0:
code: "rest_forbidden"
data: {status: 401}
message: "Sorry, you are not allowed to do that."
So the 401 status code means: unauthorised
Seems like you are not allowed to access the data via rest api.
There could be multiple reasons for that:
The image is attached to an unpublished post. The post may have the status "private" or "draft", and therefore is not public available.
Your post (and attached image) is not available for not logged in users. If there is a restriction to view your code, this also applies to rest api.
Maybe you are using some kind of membership plugin that restricts the wp rest-api. Try deactivating all plugins if one of them affects the behaviour.
You have added some custom code to restrict access rest api access.
If nothing works for you, you should look into your database and check the medias post_status.
I think it is working fine, but you do not have access to view the posts data. This is what the 401 error indicates.
To whom it may concern - In the end, I added image in WP by custom post type ( I gave up wp:featured media - I wasn't able to fetch it.) Afterward I added a code to JS -> b.querySelector(".img").setAttribute("src", a.acf.image.url); so it works:)

How do I save user account information to firebase?

I've made a Google Log in for my actions on google project, and I want to save the account info to a firestore database.
I looked at Google's example of how to do this (example here, at the very bottom under heading "Handle Data Access Requests"), but when you actually try to deploy it to firebase, you realize that it's actually has invalid syntax (or at least that's what the dialogflow inline editor is saying.....)
Here's what the error says specifically when I try to deploy this code:
The deployment of your Cloud Function failed:
Function load error: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /user_code/index.js:34
app.intent('Get Sign In', async (conv, params, signin) => {
^
SyntaxError: Unexpected token (
Any suggestions?
Thanks for the help!
Please note: I am using only the code that the tutorial has said to PLUS
I added the actions on google library and the fulfillment line (ie:
// Other libraries...
const {
dialogflow,
BasicCard,
Permission,
Suggestions,
Carousel,
SignIn
} = require('actions-on-google');
// ** code from tutorial / link **
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app)
I figured out how to do this, however it was a different method than the actions on google example. If anyone knows how to do this easier or knows what was wrong with the code in the link I posted (if anything..) please let me know / add an answer!
I decided to just write to firestore directly and put it under a "Get Signin" function (also mentioned in the tutorial for dialogflow).
Here is the function I used to get the user to sign in and also log the information into firestore:
app.intent('Get Signin', (conv, params, signin) => {
if (signin.status === 'OK') {
const payload = conv.user.profile.payload;
conv.ask(`Welcome back ${payload.name}. What can I help you with??`);
const databaseEntry = conv.user.profile.payload; // Account info, loaded to firestore
const accountRef = db.collection('Accounts').doc('account_info'); //How you want the info in firestore to appear
return db.runTransaction(t => {
t.set(accountRef, {entry: databaseEntry});
return Promise.resolve('Write complete');
}).then(doc => {
}).catch(err => {
console.log(`Error writing to Firestore: ${err}`);
});
} else {
conv.close(`To continue, you need to make an account with the app.`);
}

Use Javascript to list all Twilio Messages

I am using the Twilio Node.js documentation here:
https://www.twilio.com/docs/api/rest/message#list-get-filters
I am simply trying to list all of the messages in the message log for an account. Following the example here:
var accountSid = 'your_sid';
var authToken = "your_auth_token";
var client = require('twilio')(accountSid, authToken);
client.messages.list(function(err, data) {
data.messages.forEach(function(message) {
console.log(message.body);
});
});
Instead I get an error back:
/home/ubuntu/workspace/twilio/app.js:9
data.messages.forEach(function(message) {
^
TypeError: Cannot read property 'forEach' of undefined
at /home/ubuntu/workspace/twilio/app.js:9:18
at /home/ubuntu/workspace/twilio/node_modules/q/q.js:1547:13
at Promise_done_fulfilled (/home/ubuntu/workspace/twilio/node_modules/q/q.js:835:31)
at Fulfilled_dispatch [as dispatch] (/home/ubuntu/workspace/twilio/node_modules/q/q.js:1229:9)
at Pending_become_eachMessage_task (/home/ubuntu/workspace/twilio/node_modules/q/q.js:1369:30)
at RawTask.call (/home/ubuntu/workspace/twilio/node_modules/asap/asap.js:40:19)
at flush (/home/ubuntu/workspace/twilio/node_modules/asap/raw.js:50:29)
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
Note sure why it is not finding anything. I would simply like to print all messages in the message logs or from a specific phone number.
Twilio's documentation is no longer accurate for NodeJS, the correct way to pull up list of all messages is through the following:
client.messages.list(function(err, data) {
//notice we have removed 'messages'
data.forEach(function(message){
console.log(message.body);
});
});
Twilio developer evangelist here.
We recently released version 3 of our Node library. I believe you are still looking at the documentation for version 2. You can change this in the Twilio documentation by checking the top right of the code samples and selecting 3.x instead of 2.x.
The up to date documentation for version 3 suggests you use code like this:
client.messages.each((message) => console.log(message.body));
Let me know if that helps at all.

YouTube Search Node.js with Google API

I am trying to do a YouTube search with the Google APIs within Node.
I am using this as somewhat of a tutorial:
https://github.com/google/google-api-nodejs-client/#google-apis-nodejs-client
I have some of the basics working:
var google = require('googleapis');
var YOUTUBE_API_KEY = "--YOUR_API_KEY";
var youtube = google.youtube('v3');
var requests = youtube.search.list({part:'snippet', q: 'cats', maxResults: 10});
When I call this I get this message:
Error: Daily limit for Unauthenticated Used Exceeded.
Now, this is obviously because I'm not using my API key. However, I cannot find any resource out there that shows you how the API key is used for Node.
Anything I find tells me to do something like this:
var YOUTUBE_CLIENT_KEY = '';
var CLIENT_SECRET = '';
var REDIRECT_URL = '/';
var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
google.options({auth: oauth2Client});
Followed by my "youtube.search.list...".
The issue with this approach is I have NO idea where to get:
CLIENT_ID
CLIENT_SECRET
REDIRECT_URL
I cannot find any of these anywhere online. I have tried to just follow Javascript tutorials, since Node obviously uses Javascript, although it always requires the oAuth2... which requires the three things above.
Any helps/hints?
You should input api key in the auth parameter. This is the right way of doing it as per the https://github.com/google/google-api-nodejs-client/#google-apis-nodejs-client sample docs.
var google = require('googleapis');
var youtube = google.youtube({
version: 'v3',
auth: "your-api-key"
});
youtube.search.list({
part: 'snippet',
q: 'your search query'
}, function (err, data) {
if (err) {
console.error('Error: ' + err);
}
if (data) {
console.log(data)
}
});
Thanks
It took me a while to find this point but I am a beginner on node.js and googleapis, so if someone else would like to expand this answer they can.
I believe to set simply using the Simple API key, just warning that you should only keep this key on the server side. Never give this key out, or it can be abused!
var googleapis = require('googleapis');
googleapis.client.setApiKey('YOUR API KEY');
Found this via https://developers.google.com/api-client-library/javascript/features/authentication
under Simple access using the API key

Categories