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

ERR_INVALID_URL on new URL #1437

Closed
HeadFox opened this issue Nov 12, 2018 · 1 comment
Closed

ERR_INVALID_URL on new URL #1437

HeadFox opened this issue Nov 12, 2018 · 1 comment
Assignees
Labels
help wanted We'd love to have community involvement on this issue. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. type: question Request for information or clarification. Not an issue.

Comments

@HeadFox
Copy link

HeadFox commented Nov 12, 2018

Environment details

  • OS: MacOS 10.14
  • Node.js version: v11.1.0
  • npm version: 6.4.1
  • googleapis version: v35.0.0

Steps to reproduce

  1. ? use https://github.com/gsuitedevs/node-samples/blob/master/drive/quickstart/index.js
  2. ? node index.js

When I try to authenticate with the quickstart script it gave me an error ERR_INVALID_URL. I noticed that starting url (for example : http://localhost:8000) is missing in the new URL constructor.

Original code :

    const server = http
      .createServer(async (req, res) => {
        try {
          if (req.url.indexOf('/oauth2callback') > -1) {
            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.get('code'));
            oauth2Client.credentials = tokens;
            resolve(oauth2Client);
          }
        } catch (e) {
          reject(e);
        }
      })
      .listen(3000, () => {
        opn(authorizeUrl, {wait: false}).then(cp => cp.unref());
      });

Modified code :

const server = http
    .createServer(async (req, res) => {
      try {
          if (req.url.indexOf('/oauth2callback') > -1) {
			// Now use keys origin url
            const qs = new url.URL(keys.javascript_origins[0] + req.url).searchParams; 
            res.end('Authentication successful! Please return to the console.');
            server.destroy();
            const {tokens} = await oauth2Client.getToken(qs.get('code'));
            oauth2Client.credentials = tokens;
            resolve(oauth2Client);
          }
        } catch (e) {
          reject(e);
        }
      })
      .listen(8000, () => {
        opn(authorizeUrl, {wait: false}).then(cp => cp.unref());
      });
@JustinBeckwith JustinBeckwith added triage me I really want to be triaged. type: question Request for information or clarification. Not an issue. help wanted We'd love to have community involvement on this issue. and removed triage me I really want to be triaged. labels Nov 13, 2018
@AVaksman
Copy link
Contributor

@HeadFox thank you for pointing it out
try to use the following:

const server = http
        .createServer(async (req, res) => {
          try {
            if (req.url.indexOf('/oauth2callback') > -1) {
              const qs = querystring.parse(url.parse(req.url).query);
              res.end('Authentication successful! Please return to the console.');
              server.destroy();
              const {tokens} = await oauth2Client.getToken(qs.code);
              oauth2Client.credentials = tokens;
              resolve(oauth2Client);
            }
          } catch (e) {
            reject(e);
          }
        })
        .listen(3000, () => {
          // open the browser to the authorize url to start the workflow
          opn(authorizeUrl, {wait: false}).then(cp => cp.unref());
        });

also please refer to the drive/quickstart.js sample.

@JustinBeckwith JustinBeckwith added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. priority: p2 Moderately-important priority. Fix may not be included in next release. labels Nov 27, 2018
@JustinBeckwith JustinBeckwith self-assigned this Feb 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted We'd love to have community involvement on this issue. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

3 participants