Can I Import googleapis? Google API

I need to use googleapis to communicate with google directly from my playcanvas.

is there a way to do it?

const { google } = await import(‘googleapis’);

this has an error

TypeError: Failed to resolve module specifier ‘googleapis’

This isn’t related to PlayCanvas. Clean your NPM modules folder and try again? You can ask on their repository.

1 Like

@LeXXik Hey, Lex, Thank you for hinting me. I don’t know how I can clean my NPM modules in my playcanvas editor? Can you elaborate this? Much appreciated in advance.

1 Like

I have no idea what googleapis is or where and how you are importing it. From the snippet that you have provided, I assume it is some sort of module. It is probably a Node.js based library module and Editor doesn’t support those.

My suggestion was based on assumption that you are importing it in your engine-only Node based project (without Editor). You should have a folder called node_modules in your project after you run npm install googleapis or whatever the name of the library you are installing.

1 Like

Check this page:

You’ll notice it says:

Node.js client library for using Google APIs.

So it’s designed for use with Node. If you want to use it in the browser (e.g. with PlayCanvas), you’d need a UMD build. However, the ‘googleapis’ project doesn’t provide this ‘out of the box’.

Note that PlayCanvas doesn’t yet play nice with ES Module based codebases (although it’s very much in the works).

Its worth noting that the Google API library is supposed to be used on your server, not running in client browser. Your server, containing secret API keys, should authenticate with Google, while browsers authenticate with your server by your own means.

As @will mentioned, those libs are Node based, which won’t work in the browser, but there are numerous other libraries that do, but for the moment you’ll need to grab a UMD bundle and manually link to it. If you can’t find an official UMD bundle, it’s work checking something like jsdelivr.com ore esm.sh which are 3rd party cdn’s that do provide bundles of popular JS libraries.

I appreciate it’s not an ideal workflow, but we are actively working on full native support for ES Modules, which you can track here

1 Like

A post was split to a new topic: What am I not allowed to do?

Google provides API’s for their services. They have major considerations about these API’s being used on a “front site” that pretends they are providing a service while Google is servicing the entire request. When an API is used from the client browser, it would require either one of:

  • Client logged into Google with API permissions set and billing enabled
  • Server provides the client browser with Google API access credentials

In the first situation, you are essentially asking the user to pay for the API access. Yes, there is a free tier but after that, the API will throttle requests, deny access, or bill based on usage metrics. Nobody would want to use your site.

In the second situation, you are paying for the user to access the Google API’s in an unrestricted fashion. Not only does the user not have much accountability, but you would end up paying for malicious users to potentially slam Google servers with DOS attacks or use your API keys for data farming. And you would be accountable for it because Google has Over 900 lawyers.

Now, when your nodejs server keeps secret the API keys as a shared secret with the millions, and millions, of the Google servers - you can throttle requests from users so that you are not overwhelming said servers so you control how much you spent on Google APIs. There is also the understanding and fact that you have the logs of all the users who, by virtue of a ToS existing, understand that they are in fact responsible for things they send to your site. I know people actually do not read and/or understand those things but that’s the world we live in. Corporations are many while the user is only one - in order to negotiate terms users would need to do something like a class action lawsuit just to have the privilege of communication with a company. You see other companies negotiate with each other every deal is made with the understanding that anybody who knowingly does not follow the terms and conditions may have their freedom and rights taken away. For the sake of freedom and human rights, of course.

For more information see https://en.wikipedia.org/wiki/Corporate_personhood:

In most countries, a corporation has the same rights as a natural person to hold property, enter into contracts, and to sue or be sued. Granting non-human entities personhood is a Western concept applied to corporations.

Idk what kinda person has many bodies or zero bodies depending on how you look at it, that’s the part of the ever-changinge social contract that you agreed to when you were too young to enter into any legally binding contracts or have other rights that a corporation obtains immediately upon incorporation with the state, not after 18 years. The double standard between corporations and people can be frustrating, but it is something we just have to deal with until more people realize its an oligarchy, which is exactly what the wars were about. but people gave away their rights to end the wars and because of that the leadership has declared that we are not deserving of rights for that very reason:

https://www.goodreads.com/quotes/140634-those-who-would-give-up-essential-liberty-to-purchase-a

It’s sad but true. I apologize for the tangent but it really is a question of ethics. This post does not necessarily reflect the opinion of any person, real or imagined, living or dead. Thanks

1 Like