How do you locate and delete the auth information that google saves to your disk file to connect to the api with a different email? NODE.JS on MAC OS - node.js

I ran a test run to learn how to connect to google calendar api via node.google auth with node js
I re-walked through the steps to attempt to connect my actual project to google calendar api, but when i run node quickstart.js i get this error:
The API returned an error: Error: unauthorized_client
after hours of deliberating I figured out that the Google client library was saving my auth info on my disk. So, when i ran node quickstart.js, I received an error because my new client_secret.json that I created for my actual project, did not match the info in my client_secret.json that was created in my test run.
A doc suggestion that I found that i think is relevant:
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/calendar-nodejs-quickstart.json
but when I navigate to my user folder I do not have a .credentials directory, thus I'm unable to locate and delete my old auth info.
I tried searching for oauth via global search and found a few oauth.js files that I thought were the issue. I naively deleted all of them and then re ran the command only to find the terminal yell at me for not having an oauth node module.
After more hours of replacing the oauth files I deleted I'm still unable to locate or delete the troublesome auth info.
Has anyone encountered a similar problem while programming on a mac and connecting to Google API thru node?

Assuming your code is following the quick start. You can find the credeitlals for the user
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + '/.credentials/';
If you change it to (or the equivalent to a mac directory structure not mac person sorry)
var TOKEN_DIR = c:\currentdir\credentials;
It should request access again and store the new credentials file there
Note:
It looks like the way the code works they are expecting you to have the .json file in that directory. So remember to put it in what ever directory you have TOKEN_DIR set to.
var TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json';

The problem I had was finding that credentials directory. I learned that it was a hidden folder and learned how to show / hide hidden folders. For any future problem havers the answer is to navigate to /Users/YOURUSERPROFILE in finder and pressing: CMD + SHIFT + . to show the hidden credential folder

Related

Google Cloud Natural Language Example

I have followed the getting started page closely.
https://cloud.google.com/natural-language/docs/reference/libraries#client-libraries-install-php
The example code has the following: $projectId = 'YOUR_PROJECT_ID';
I fill in my project id taken from the json file and the Google console--e.g. "$projectID = 'myproject-197218'" and I always get a fatal error with "Permission Denied."
I have set the env variable, run composer to install the library. And, I created the Google json file. I am running the example in PHP code.
I am running the code on my local server (xampp).
I figured out my problem. The Google Cloud json file was stored on my drive d:, so in the env variable I referenced it as 'GOOGLE_APPLICATION_CREDENTIALS= d:\xampp\htdocs\googapi\mproj.json', it did not work; when I moved it to the root of the c: drive and referenced it there (GOOGLE_APPLICATION_CREDENTIALS=c:proj.json), it worked fine.
Are you sure that the ID of your project is that one? I'm working in Google Cloud and I cannot see this project ID in our database, but if I type "my-project-197218" with a "-" between "my" and "project" I am able to find one project. please, to make sure that this is your correct project ID, run this command in your Google Cloud Shell to get the default project ID:
gcloud config list --format 'value(core.project)' 2>/dev/null

How to connect to Realm from aws lambda node

I am trying to connect to a Realm instance from an aws lambda (node) with the following code:
await Realm.Sync.User.login('https://server.realm.io', 'username', 'password')
.then((user) => {
let config = user.createConfiguration();
config.schema = [Schema];
Realm.open(config).then((realm) => {
//Do some cool stuff });
The problem is it tries to create a directory realm-object-server when it logins. As we know the lambdas file system is read-only except for the tmp folder. Is there a way to tell realm to write this realm-object-server to the tmp folder or is there a way to login that doenst create a direcotry at all?
Thanks in advance for the help
I ran into the same issue using Realm from a Google Cloud Function. None of the Realm configuration options like path or inMemory seemed to have any effect. After much digging, the solution I found was to call process.chdir('/tmp') prior to opening the Realm. This changes the current NodeJS process's working directory as explained in the NodeJS documentation. This allowed me to open the realm successfully.

How can I host my own Parse Server on Heroku using MongoDB? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
(To be clear, Im asking this question so as to provide the answer I found in an effort to help others that have been similarly affected by the Parse closure)
Parse.com recently anounced that they are closing shop January 2017 but thankfully they have made their Parse Server software open source so we can all host our own Parse server instances.
I have looked into the various options available for hosting Parse Server and have decided to host mine on Heroku
I loved Parse because it was so easy to use and I have no real experience with setting up a backend. I have tried to follow several guides online but found them all a bit hard to understand with vague steps or steps that take you down a rabbit hole on another site installing a bunch of tools and it all gets a bit confusing.
How can I host Parse Server on heroku, set it up to accept cross domain requests from my application, create a MongoDB database, and migrate all of my data from Parse to the new database?
Self hosting Parse Server on Heroku
Setup Heroku, MongoDB, and Parse
Visit https://signup.heroku.com/ and sign up for an acount
Verify your email, and log into Heroku
Go to Deploying a Parse Server to Heroku
Click "Deploy to Heroku"
Give your parse server a name
Skip the app's APP_ID and MASTER_KEY for now, we'll set that in later step.
Click "Deploy for free"
If the below box appears, enter your credit card info to verify your account. Dont worry, you wont be charged unless you upgrade your account later. This is simply a security measure Heroku has in place to prevent abuse.
Heroku will create your app. In the background it will also create a .git repo for you and clone the contents of the official parse-server-example git repo, create a MongoDB database, and configure your Heroku app to use that DB.
When the app is created, you'll be taken to it's Heroku dashboard or you can get there by logging in, clicking "Personal Apps" then clicking on the name of your parse server
On your app's dashboard, click "Settings" then click "Reveal config vars"
Fill in your app's APP_ID and MASTER_KEY. If you have an app on Parse.com now, you can use the same keys it uses. You can gey the at https://www.parse.com/apps/<APP_NAME>/edit#keys where <APP_NAME> is the name of your app. If you're creating a new app you can generate random keys here. You can add new keys for any sdks you plan to use, I'm adding one for the javascript sdk here.
Make note of the value for MONGOLAB_URI, you'll need this later if you want to migrate your existing Parse data to your new DB
Scroll down and make a note of the Git Url and the Heroku domain for your app, you'll need these later
Install the Heroku Toolbelt which is a command line interface tool for managing your Heroku apps.
Open a terminal prompt and enter $ heroku login, enter your Heroku email and password
Enter $ heroku git:clone -a <MY_APP_NAME> (where <MY_APP_NAME> is the name of your heroku app) to make a local copy of your server code repo, you can also use the git url from we saw earlier. When you clone the repo you will see the message "warning: You appear to have cloned an empty repository.". The app folder will be created but it will only contain the standard git files and no parse server. To fix this. Go to the official parse-server-example git repo, download the zip file of the repo. Extract the folder, drag all of the files and folders from the parse-server-example-master folder into your app folder.
Back in the terminal, enter $ cd <MY_APP_NAME>to move into the repo
Enable CORS (so you can use the API from other domains)
On your hard drive, find and open the local copy of the repo that we just created
In the folder, open package.json and add "cors": "*" to the dependencies like this:
.
"dependencies": {
"express": "~4.2.x",
"kerberos": "~0.0.x",
"parse": "~1.6.12",
"parse-server": "~2.0", // <= don't forget this comma
"cors": "*" // add this line
}
NOTE
Make sure to remove the above comments in your actual code as it wont be valid JSON
Open index.js and make these changes:
.
var express = require('express'); // find this line in the file
var cors = require('cors') // add this line below it
//....
//var databaseUri = process.env.DATABASE_URI || process.env.MONGOLAB_URI // old name, you may need to change this for new deployments
var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI // changed to MONGODB_URI in Heroku!
//....
var app = express(); // find this line in the file
app.use(cors()); // add this line below it
//Add declarations for any keys you plan to use as shown below
var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'myAppId',
masterKey: process.env.MASTER_KEY || '',
serverURL: process.env.SERVER_URL || 'http://localhost:1337',
javascriptKey: process.env.JAVASCRIPT_KEY || '', //** add this line no need to set values, they will be overwritten by heroku config vars
restAPIKey: process.env.REST_API_KEY || '', //** add this line
dotNetKey: process.env.DOT_NET_KEY || '', //** add this line
clientKey: process.env.CLIENT_KEY || '', //** add this line
});
Go back to the terminal window, make sure you are still in the folder of the repo we cloned and enter the below to push the changes to Heroku:
.
$ git add .
$ git commit -am "make it better"
$ git push heroku master
Test your new Parse Server
Go to this jsFiddle page
Change <MY_APP_ID>,<MY_JS_KEY>,<MY_HEROKU_APP_NAME> in the fiddle to the appropriate values for your app then click "Run"
.
Parse.initialize('<MY_APP_ID>', '<MY_JS_KEY>');
Parse.serverURL = 'https://<MY_HEROKU_APP_NAME>.herokuapp.com/Parse'
You should get the below alert letting you know that your new Parse server is working correctly
NOTE:
If, you use the jsfiddle tool with multiple parse server instances, you might get the error "invalid session token". If this happens, open the dev console, and delete all of the "parse" keys from local storage, after that, it should work:
Migrate your existing data from Parse
You should do this at least once with a test app before migrating a production app. Also, it seems that legacy files from your old app may not yet transfer, see this GitHub Issue
Before we migrate, if your new MongoDB has data in it the migration will fail. If you just tested the server and created and object, go to https://dashboard.heroku.com/apps/<MY_HEROKU_APP_NAME>/resources, click "MongoLab" next to it's icon, then, on the next page, click "Delete all collections"
Go to https://dashboard.parse.com/apps/<APP_NAME>/settings/general where <APP_NAME> is the name of your parse app
Scroll down on the page and click "Migrate"
In the modal, in the grey area (that doesn't look much like an input...) enter the MONGOLAB_URI that we made note of earlier
Click "Begin the migration"
When the migration is done, click "Finalize" then, in the popup click "Okay" when the migration is final, all your data will be in your new MongoDB database.
For now, your Parse dashboard will continue to show your data that now lives in the new DB. To test everything, go back to the test jsFiddle we used before and run it again. Now, go to your Parse dashboard and you should see the newly added class and row. Note that when I used the new Parse dashboard, I could see the number of rows but the row area was all blank. I reverted to the old Parse dashboard and could see everything fine.

Desktop Top Save Location For User Storage?

While writing a node-webkit application I came across needing to save users uploaded photos through the built in html file input. I can save photos easy enough where I wish via a nice post here on node file uploading and node-webkit's file dialog changes simple enough.
The question really is there a best practice for saving user generated content for a desktop app or is the application folder (OS specific) reasonable to use with say the application or company name? Security is not much of a concern here in this case.
node-webkit (at this time) has an application folder under under process.env.LOCALAPPPATH (for windows users anyway) which could be used.
Another option which is viable is to use an application directory in the exe directory of the program.
IE for node with process global.
var path = require('path');
var appDir = path.dirname( process.execPath ) + path.sep + 'data' + path.sep;
//might produce something like c:\\programs\\node-webkit\\data\\

Temporary File Download

Is there a service that creates basically a one-time download of a file, preferably something I can use from NodeJS?
I've done some research on FilePicker, and haven't found anything about regenerating the link it gives you for a file. There may be a way to do this with NodeJS, but I'm using Meteor at the same time so many Node things probably will conflict.
You could build it with meteor. Using meteor-router with meteorite & use server side routing to deliver the files.
You need a collection to keep track of downloaded files:
Server JS
var downloads = new Meteor.Collection("downloads");
//create a link
downloads.insert({url:"/mydownload.zip",downloaded:false})
Meteor.Router.add('/file/:id', 'GET', function(id) {
download = downloads.findOne(id);
if( download) {
if(dowload.downloaded) {
this.response.send("You've already downloaded me")
}
else
{
//I guess you could just redirect or stream the file for an extra layer of surety
this.response.redirect(download.url);
}
}
});
On the client you can use /files/{{_id}} with _id of the file from downloads the person has as the link
My recommendation would also be to add custom server-side logic to count # of uploads (or just flag a file as downloaded/not downloaded) and respond accordingly. The closest you could do with Filepicker.io would be using the security policies to restrict downloading the file to a specific time interval.
in addition to using the router package
in Meteor.startup you can add
var require = __meteor_bootstrap__.require;
fs = require( 'fs' );
the fs variable should be declared on the server only. the fs package is used by Meteor and does not need to be added separately.
once you have done this, you can create files with Meteor.uuid() as their name which makes them unique and very difficult to guess. It is also possible to delete the file after a certain amount of time by using Meteor.setTimeout
the question is: where do the files to be downloaded come from?
Solution using Heroku Cloud and NodeJS Meteor Hooks
Heroku in particular is actually great for temporary file download links: they offer a "temporary scratchpad" filesystem that is reset every time the program restarts, and each running Node server cannot see the files other instances have created.
Each dyno gets its own ephemeral filesystem, with a fresh copy of the
most recently deployed code. During the dyno’s lifetime its running
processes can use the filesystem as a temporary scratchpad, but no
files that are written are visible to processes in any other dyno and
any files written will be discarded the moment the dyno is stopped or
restarted.
Taken from the Heroku documentation: https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem
Thus, any files written to the "filesystem" will be temporary.
This allows for a very easy solution to this problem: you can simply use NodeJS filesystem manipulation to create temporary files on the server, serve them once (or for a limited time), and then remove them so they cannot be downloaded again.
This in combination with something like $.download() will make a seamless experience which in turn prevents unauthorized downloads.

Resources