Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot authenticate using `private_key` and `client_email` #1173

Closed
eduardo-tenorio-guiabolso opened this issue Mar 21, 2016 · 6 comments

Comments

@eduardo-tenorio-guiabolso
Copy link

commented Mar 21, 2016

I am trying to connect my NodeJS application to Pub/Sub. Since the production environment is in an AWS instance, I cannot use the default credentials approach. So, my approach is to pass a credentials object with the private key and the client email:

var gcloud_config = {
    projectId: process.env.GCLOUD_PROJECT_ID,
    credentials: {
        client_email : process.env.GCLOUD_CLIENT_EMAIL,
        private_key : process.env.GCLOUD_PRIVATE_KEY
    }
}

According to the docs. This variable is passed as an argument in a class constructor:

constructor(gcloud_config) {
        this.topicName = 'cerebro.TrackeableEvent';

        this.pubsub = gcloud.pubsub(gcloud_config);
        this.topic = this.pubsub.topic(this.topicName);
        this.topic.get({
            autoCreate: true
        }, function(err, data) {
            console.log('PubSubTrackeableEventAPI');
            console.log('err: ' + err);
            console.log('data: ' + data);
        });
    }

When I run locally, everything is OK. There is no err and I can publish to this.topicName. When I run on my staging environment, at AWS, I receive the error Error: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information.. I don't know what I am doing wrong. I tried several combinations of the GCLOUD_PRIVATE_KEY. With and without the markers "-----BEGIN PRIVATE KEY-----\n" and "\n-----END PRIVATE KEY-----\n".

Is this a bug or am I doing something wrong that I don't know?

@eduardo-tenorio-guiabolso

This comment has been minimized.

Copy link
Author

commented Mar 21, 2016

I guess I found the error. Tomorrow I'll check at the office.

@eduardo-tenorio-guiabolso

This comment has been minimized.

Copy link
Author

commented Mar 21, 2016

Changed

private_key : process.env.GCLOUD_PRIVATE_KEY

by

private_key : process.env.GCLOUD_PRIVATE_KEY.replace(/\\n/g, '\n')
@andershagebakken

This comment has been minimized.

Copy link

commented Feb 11, 2017

@biozork

This comment has been minimized.

Copy link

commented Jun 28, 2017

@eduardo-tenorio-guiabolso I had the same challenge with hosting on Heroku, I needed to store the private key in environment variable, and your fix saved my day (doing the newline .replace on the key)

Thank you so much :)

@UncertainConstraint

This comment has been minimized.

Copy link

commented Mar 11, 2019

@eduardo-tenorio-guiabolso I have been banging my head against a wall trying to figure out how to handle the newline characters, thank you so much for posting your solution, it worked perfectly!

@kavunshiva

This comment has been minimized.

Copy link

commented Mar 25, 2019

(╯°□°)╯︵ ┻━┻

...but yeah, this definitely does the trick. thanks a million!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.