google-api-nodejs-client is not fetching details
up vote
0
down vote
favorite
I am using google-api-nodejs-client oauth2.js and console is not showing any errors but no profile details are getting fetched of the signed in user in console. Here, is my code where "Hey there.2" is the last line that is getting displayed in console. Can someone point me the incorrect part?
I have included oauth2.keys.json in the directory.
'use strict';
const fs = require('fs');
const path = require('path');
const http = require('http');
const url = require('url');
const opn = require('opn');
const destroyer = require('server-destroy');
const {google} = require('googleapis');
const plus = google.plus('v1');
console.log("Hey there.1");
/**
* To use OAuth2 authentication, we need access to a a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI. To get these credentials for your application, visit https://console.cloud.google.com/apis/credentials.
*/
const keyPath = path.join(__dirname, 'oauth2.keys.json');
let keys = {redirect_uris: ['']};
if (fs.existsSync(keyPath)) {
keys = require(keyPath).web;
}
/**
* Create a new OAuth2 client with the configured keys.
*/
const oauth2Client = new google.auth.OAuth2(
keys.client_id,
keys.client_secret,
keys.redirect_uris[0]
);
/**
* This is one of the many ways you can configure googleapis to use authentication credentials.
In this method, we're setting a global reference for all APIs.
Any other API you use here, like google.drive('v3'), will now use this auth client.
You can also override the auth client at the service and method call levels.
*/
google.options({auth: oauth2Client});
/**
* Open an http server to accept the oauth callback.
The only request to our webserver is to /callback?code=<code>
*/
async function authenticate(scopes) {
return new Promise((resolve, reject) => {
// grab the url that will be used for authorization
const authorizeUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: scopes.join(' '),
});
console.log("Hey there.2");
const server = http
.createServer(async (req, res) => {
try {
console.log("Hey there.3");
if (req.url.indexOf('google') > -1) {
//const qs = querystring.parse(url.parse(req.url).query);
const qs = new url.URL(req.url).searchParams;
res.end('Authentication successful! Please return to the console.');
server.destroy();
//const {tokens} = await oauth2Client.getToken(qs.code);
const {tokens} = await oauth2Client.getToken(qs.get('code'));
oauth2Client.credentials = tokens;
resolve(oauth2Client);
console.log("Hey there.4");
}
} catch (e) {
console.log(e);
reject(e);
}
})
.listen(3000, () => {
// open the browser to the authorize url to start the workflow
opn(authorizeUrl, {wait: false}).then(cp => cp.unref())
;
});
destroyer(server);
});
}
async function runSample() {
// retrieve user profile
const res = await plus.people.get({userId: 'me'});
console.log(res.data);
console.log("Hey there.5");
console.log(userId);
}
const scopes = ['https://www.googleapis.com/auth/plus.login'];
authenticate(scopes)
.then(client => runSample(client))
.catch(console.error);
node.js
New contributor
aviralu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
favorite
I am using google-api-nodejs-client oauth2.js and console is not showing any errors but no profile details are getting fetched of the signed in user in console. Here, is my code where "Hey there.2" is the last line that is getting displayed in console. Can someone point me the incorrect part?
I have included oauth2.keys.json in the directory.
'use strict';
const fs = require('fs');
const path = require('path');
const http = require('http');
const url = require('url');
const opn = require('opn');
const destroyer = require('server-destroy');
const {google} = require('googleapis');
const plus = google.plus('v1');
console.log("Hey there.1");
/**
* To use OAuth2 authentication, we need access to a a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI. To get these credentials for your application, visit https://console.cloud.google.com/apis/credentials.
*/
const keyPath = path.join(__dirname, 'oauth2.keys.json');
let keys = {redirect_uris: ['']};
if (fs.existsSync(keyPath)) {
keys = require(keyPath).web;
}
/**
* Create a new OAuth2 client with the configured keys.
*/
const oauth2Client = new google.auth.OAuth2(
keys.client_id,
keys.client_secret,
keys.redirect_uris[0]
);
/**
* This is one of the many ways you can configure googleapis to use authentication credentials.
In this method, we're setting a global reference for all APIs.
Any other API you use here, like google.drive('v3'), will now use this auth client.
You can also override the auth client at the service and method call levels.
*/
google.options({auth: oauth2Client});
/**
* Open an http server to accept the oauth callback.
The only request to our webserver is to /callback?code=<code>
*/
async function authenticate(scopes) {
return new Promise((resolve, reject) => {
// grab the url that will be used for authorization
const authorizeUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: scopes.join(' '),
});
console.log("Hey there.2");
const server = http
.createServer(async (req, res) => {
try {
console.log("Hey there.3");
if (req.url.indexOf('google') > -1) {
//const qs = querystring.parse(url.parse(req.url).query);
const qs = new url.URL(req.url).searchParams;
res.end('Authentication successful! Please return to the console.');
server.destroy();
//const {tokens} = await oauth2Client.getToken(qs.code);
const {tokens} = await oauth2Client.getToken(qs.get('code'));
oauth2Client.credentials = tokens;
resolve(oauth2Client);
console.log("Hey there.4");
}
} catch (e) {
console.log(e);
reject(e);
}
})
.listen(3000, () => {
// open the browser to the authorize url to start the workflow
opn(authorizeUrl, {wait: false}).then(cp => cp.unref())
;
});
destroyer(server);
});
}
async function runSample() {
// retrieve user profile
const res = await plus.people.get({userId: 'me'});
console.log(res.data);
console.log("Hey there.5");
console.log(userId);
}
const scopes = ['https://www.googleapis.com/auth/plus.login'];
authenticate(scopes)
.then(client => runSample(client))
.catch(console.error);
node.js
New contributor
aviralu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
What is the Google server returning for tokens at this line:oauth2Client.credentials = tokens;?
– John Hanley
11 hours ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am using google-api-nodejs-client oauth2.js and console is not showing any errors but no profile details are getting fetched of the signed in user in console. Here, is my code where "Hey there.2" is the last line that is getting displayed in console. Can someone point me the incorrect part?
I have included oauth2.keys.json in the directory.
'use strict';
const fs = require('fs');
const path = require('path');
const http = require('http');
const url = require('url');
const opn = require('opn');
const destroyer = require('server-destroy');
const {google} = require('googleapis');
const plus = google.plus('v1');
console.log("Hey there.1");
/**
* To use OAuth2 authentication, we need access to a a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI. To get these credentials for your application, visit https://console.cloud.google.com/apis/credentials.
*/
const keyPath = path.join(__dirname, 'oauth2.keys.json');
let keys = {redirect_uris: ['']};
if (fs.existsSync(keyPath)) {
keys = require(keyPath).web;
}
/**
* Create a new OAuth2 client with the configured keys.
*/
const oauth2Client = new google.auth.OAuth2(
keys.client_id,
keys.client_secret,
keys.redirect_uris[0]
);
/**
* This is one of the many ways you can configure googleapis to use authentication credentials.
In this method, we're setting a global reference for all APIs.
Any other API you use here, like google.drive('v3'), will now use this auth client.
You can also override the auth client at the service and method call levels.
*/
google.options({auth: oauth2Client});
/**
* Open an http server to accept the oauth callback.
The only request to our webserver is to /callback?code=<code>
*/
async function authenticate(scopes) {
return new Promise((resolve, reject) => {
// grab the url that will be used for authorization
const authorizeUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: scopes.join(' '),
});
console.log("Hey there.2");
const server = http
.createServer(async (req, res) => {
try {
console.log("Hey there.3");
if (req.url.indexOf('google') > -1) {
//const qs = querystring.parse(url.parse(req.url).query);
const qs = new url.URL(req.url).searchParams;
res.end('Authentication successful! Please return to the console.');
server.destroy();
//const {tokens} = await oauth2Client.getToken(qs.code);
const {tokens} = await oauth2Client.getToken(qs.get('code'));
oauth2Client.credentials = tokens;
resolve(oauth2Client);
console.log("Hey there.4");
}
} catch (e) {
console.log(e);
reject(e);
}
})
.listen(3000, () => {
// open the browser to the authorize url to start the workflow
opn(authorizeUrl, {wait: false}).then(cp => cp.unref())
;
});
destroyer(server);
});
}
async function runSample() {
// retrieve user profile
const res = await plus.people.get({userId: 'me'});
console.log(res.data);
console.log("Hey there.5");
console.log(userId);
}
const scopes = ['https://www.googleapis.com/auth/plus.login'];
authenticate(scopes)
.then(client => runSample(client))
.catch(console.error);
node.js
New contributor
aviralu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I am using google-api-nodejs-client oauth2.js and console is not showing any errors but no profile details are getting fetched of the signed in user in console. Here, is my code where "Hey there.2" is the last line that is getting displayed in console. Can someone point me the incorrect part?
I have included oauth2.keys.json in the directory.
'use strict';
const fs = require('fs');
const path = require('path');
const http = require('http');
const url = require('url');
const opn = require('opn');
const destroyer = require('server-destroy');
const {google} = require('googleapis');
const plus = google.plus('v1');
console.log("Hey there.1");
/**
* To use OAuth2 authentication, we need access to a a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI. To get these credentials for your application, visit https://console.cloud.google.com/apis/credentials.
*/
const keyPath = path.join(__dirname, 'oauth2.keys.json');
let keys = {redirect_uris: ['']};
if (fs.existsSync(keyPath)) {
keys = require(keyPath).web;
}
/**
* Create a new OAuth2 client with the configured keys.
*/
const oauth2Client = new google.auth.OAuth2(
keys.client_id,
keys.client_secret,
keys.redirect_uris[0]
);
/**
* This is one of the many ways you can configure googleapis to use authentication credentials.
In this method, we're setting a global reference for all APIs.
Any other API you use here, like google.drive('v3'), will now use this auth client.
You can also override the auth client at the service and method call levels.
*/
google.options({auth: oauth2Client});
/**
* Open an http server to accept the oauth callback.
The only request to our webserver is to /callback?code=<code>
*/
async function authenticate(scopes) {
return new Promise((resolve, reject) => {
// grab the url that will be used for authorization
const authorizeUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: scopes.join(' '),
});
console.log("Hey there.2");
const server = http
.createServer(async (req, res) => {
try {
console.log("Hey there.3");
if (req.url.indexOf('google') > -1) {
//const qs = querystring.parse(url.parse(req.url).query);
const qs = new url.URL(req.url).searchParams;
res.end('Authentication successful! Please return to the console.');
server.destroy();
//const {tokens} = await oauth2Client.getToken(qs.code);
const {tokens} = await oauth2Client.getToken(qs.get('code'));
oauth2Client.credentials = tokens;
resolve(oauth2Client);
console.log("Hey there.4");
}
} catch (e) {
console.log(e);
reject(e);
}
})
.listen(3000, () => {
// open the browser to the authorize url to start the workflow
opn(authorizeUrl, {wait: false}).then(cp => cp.unref())
;
});
destroyer(server);
});
}
async function runSample() {
// retrieve user profile
const res = await plus.people.get({userId: 'me'});
console.log(res.data);
console.log("Hey there.5");
console.log(userId);
}
const scopes = ['https://www.googleapis.com/auth/plus.login'];
authenticate(scopes)
.then(client => runSample(client))
.catch(console.error);
node.js
node.js
New contributor
aviralu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
aviralu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
aviralu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 17 hours ago
aviralu
11
11
New contributor
aviralu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
aviralu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
aviralu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
What is the Google server returning for tokens at this line:oauth2Client.credentials = tokens;?
– John Hanley
11 hours ago
add a comment |
What is the Google server returning for tokens at this line:oauth2Client.credentials = tokens;?
– John Hanley
11 hours ago
What is the Google server returning for tokens at this line:
oauth2Client.credentials = tokens; ?– John Hanley
11 hours ago
What is the Google server returning for tokens at this line:
oauth2Client.credentials = tokens; ?– John Hanley
11 hours ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
aviralu is a new contributor. Be nice, and check out our Code of Conduct.
aviralu is a new contributor. Be nice, and check out our Code of Conduct.
aviralu is a new contributor. Be nice, and check out our Code of Conduct.
aviralu is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371198%2fgoogle-api-nodejs-client-is-not-fetching-details%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the Google server returning for tokens at this line:
oauth2Client.credentials = tokens;?– John Hanley
11 hours ago