google-api-nodejs-client is not fetching details











up vote
0
down vote

favorite
1












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);









share|improve this question







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















up vote
0
down vote

favorite
1












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);









share|improve this question







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













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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);









share|improve this question







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 google-oauth google-signin






share|improve this question







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.











share|improve this question







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.









share|improve this question




share|improve this question






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


















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






aviralu is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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






























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.










 

draft saved


draft discarded


















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.















 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft