getting error in a simple api request using node JS request lib - node.js

I'm trying to preform a simple API request with node:
const request = require('request');
request({
url: 'https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=MYAPIKEY',
json: true
} , (error , response , body) => {
console.log(body);
});
and getting the following error on console:
{
error_message: 'You have exceeded your daily request quota for this API. If you did not set a custom daily request quota, verify your project has an active billing account: http://g.co/dev/maps-no-account',
results: [],
status: 'OVER_QUERY_LIMIT'
}
while on the browser the request completes successfully with the exact same request.
I activated my billing account with no change.
Any suggestions what might be the problem?

so after some digging I found out that new rules have been applied by google, and in order to get the response you need to activate billing on every project through the console.
First you need to set up billing account for google cloud platform, which won't charge you unless you want to. after you done that, you need to go to your specific project and activate billing for that project, which wasn't very intuitive to understand.

Related

Not getting data of GITHUB api using npm module sync-request

I am trying to get data of below url using sync-request module.
https://api.github.com/repos/rethinkdb/rethinkdb/stargazers
I get the data when i call it in browser or through postman.
But i am getting 403 forbidden error when calling it using sync-request in my node api.
My code looks like this.
var request = require("sync-request");
var response = request('GET', 'https://api.github.com/repos/rethinkdb/rethinkdb/stargazers', {
headers: {},
json: true
});
I am able to fetch data of many other api's but not this one. Please help.
Response body already contains the explanation:
Request forbidden by administrative rules. Please make sure your request has a User-Agent header (http://developer.github.com/v3/#user-agent-required). Check https://developer.github.com for other possible causes.
It will work like:
var response = request('GET', 'https://api.github.com/repos/rethinkdb/rethinkdb/stargazers', {
headers: { 'User-Agent': 'Request' },
json: true
});
The use of sync-request is strongly discouraged because synchronousness is achieved via a hack and may block the process for a long time.
For sequential execution request-promise can be used together with async..await.
Try to use an access token along with the GitHub API call
like this
[https://api.github.com/repos/rethinkdb/rethinkdb/stargazers?access_token=f33d1f112b7883456c990028539a22143243aea9]
As you say the API works in the browser it should not be an issue.
When you use too many calls through the GitHub API they they give the following message
{
"message": "API rate limit exceeded for 192.248.24.50. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
"documentation_url": "https://developer.github.com/v3/#rate-limiting"
}
To overcome this issue you can use an access token using an access token you can to access the private repositories in your account as well .
Here is the link for to get an access token [https://github.com/settings/developers]

Handle Stripe response in Express app

I'm trying to write a simple Stripe integration using Express. My code creates the customer and charge and completes, appearing in my dashboard. However I'm struggling to understand how to handle the response from Stripe. The following just outputs the whole response from Stripe - what I would like is to be able to output a friendly message dependent on the content of this response:
stripe.customers.create({
card: stripeToken
})
.then(customer =>
stripe.charges.create({
amount: fee,
description: "Client Ref: " + clientref,
currency: "gbp",
customer: customer.id,
metadata: {
'allocation:': allocate
}
}))
.then(charge => response.send(charge))
.catch(err => {
console.log("Error:", err);
response.status(500).send({error: "Purchase Failed"});
});
How can I look at the response and handle it accordingly? I've tried using the response.send promise and I can get it to display parts of the response, but is this a 'charge' page? How can I display this as html and not just plaintext??
I have looked in the Stripe docs and haven't found anything similar. I want to be able to account for failed payments, declines etc as well as successful charges.
Any help or other examples much appreciated!
Thanks
edit: ok I handle the 'errors' in the catch block. I didn't realise Stripe doesn't throw exceptions, so that makes life easier. My point about formatting the messages still stands however. '/charge' just outputs plain text.
How can I look at the response and handle it accordingly?
The Stripe docs clearly show you what the response for a successful charge is here: https://stripe.com/docs/api/node#create_charge (under Example response). It gives you back a large JSON object.
You need to figure out what part of that response is useful to you to display in your client-side application. If none of the information is needed, then you can simply ignore the response.
(...) but is this a 'charge' page? How can I display this as html and not just plaintext??
What you are doing now is simply sending the whole JSON object to your client via res.send. And you are not doing anything with the response other than simply sending it back as plain text. You need to configure your view to display the response: http://expressjs.com/en/guide/using-template-engines.html
Or if your producing an API for others to consume, then use res.json to send back the JSON you got back from Stripe. http://expressjs.com/en/4x/api.html#res.json
I want to be able to account for failed payments, declines etc as well as successful charges.
Then you need to handle the error that occurs. You are handling the error via .catch, but you aren't inspecting the error object. Again, the stripe have documentation on errors as well: https://stripe.com/docs/api#errors
And they even tell you how to handle errors as well: https://stripe.com/docs/api#handling-errors

How to authorize for Amazon's Alexa API?

I want to send a request to this Amazon Alexa API.
That page contains the last 50 activities I made with my Amazon Echo. The page returns JSON. Before you can request that page, you need to authorize your account, so the proper cookies are set in your browser.
If I do something simple as:
const rp = require("request-promise");
const options = {
method: "GET",
uri: "https://alexa.amazon.com/api/activities?startTime=&size=50&offset=-1",
json: true
};
rp(options).then(function(data) {
console.log(data);
}).catch(function(err) {
console.log(err);
});
I can send a GET request to that URL. This works fine, except Amazon has no idea it's me who's sending the request, because I haven't authorized my NodeJS application.
I've successfully copied ~10 cookies from my regular browser into an incognito tab and authorized that way, so I know copying the cookies will work. After adding them all using tough-cookie, it didn't work, unfortunately. I still got redirected to the signin page (according to the error response).
How do I authorize for this API, so I can send my requests?
I have been looking for a solution for this too. The best idea I have is to use account linking, but I haven't try it yet. Looks like ASK-CLI has interface for this also, but I can't figure it out how to use it (what is that URL?). For linking account to 3rd party server is not easy, but link it back to Amazon for the json API should not be that complicated.

Accessing QCMobile API

I'm currently tasked with accessing data from the Department of Transportations QCMobile API, located here.
I've made an account and have obtained my key. I've tried accessing it through Ajax calls, Node's Request and https modules, and now I'm just trying to get a response via Curl.
Every time I try to access it I get the same error: error 403, Forbidden.
My URL appears to be properly formed, as seen here:
https://mobile.fmcsa.dot.gov/qc/services/carriers/44110/basics?webKey=xxxx
When I run it from Node or from an Ajax call, I only get 403, Forbidden.
Here is my relevant Node code:
this.url = 'https://mobile.fmcsa.dot.gov/qc/services/carriers/' + dotNumber + '/basics' + '?webKey=' + this.webkey;
this.options = {
method: 'GET',
uri: this.url,
};
this.getDoTData = function() {
request(this.options)
.then(function (response) {
// Request was successful, use the response object at will
console.log(response);
})
.catch(function (err) {
// Something bad happened, handle the error
console.log(err);
});
}
When I run it via Curl, I get the same 403 with some extra detail:
curl: (56) SSLRead() return error -9806
I was wondering if anyone has any ideas as to if I'm accessing this API incorrectly. There doesn't appear to be much documentation, and the page on their site where you can submit technical questions seems to be broken.
Thanks for any insight.
This web service seems to be down for the time being. These problems started during the maintenance window on weekend of Nov 18th, and it has been in varying degrees of non-operation since then.
This is the response I got from FMCSA customer support today:
I do apologize about the inconvenience however, we have been experiencing technical difficulties with the App for a few weeks now. Our IT department is currently working on it however, at this time I do not have an exact time on when it will be fixed.

shopify - nodejs - get permanent token fails

I have written an application that talks with the shopify API. I manage to get the temporary code from shopify and redirect back to my app where I store the code to later exchange for the permanent token.
According to the docs all I need to do is then send a POST request to https://{shop}.myshopify.com/admin/oauth/access_token with the client_id, client_secret and code is the body of the request.
I am using the request module to send the request and have it set up to send the request as such:
var options = {
method: POST,
url: https://my-develop-shop.myshopify.com/admin/oauth/access_token,
json: true
};
var _body = {
"client_id": config.get('SHOP_ID'),
"client_secret": config.get('SHOP_SECRET'),
"code": tempCode
}
_body = JSON.stringify(_body);
options.body = _body;
request(options, callback);
However when I send the request it always returns with : error_description: 'Could not find Shopify API application with api_key ' }
The app is installed successfully on the client's shop, so why would this error be returned?
Is there a special header that shopify expects? The docs are so vague.
Why does it not authenticate?
Well I cheated and used the shopify-node-api package. There I just use the exchange_temporary_token method. This api also handles throttling so it's a decent investment in the time you might spend incorporating it.

Resources