ChatGPT解决这个技术问题 Extra ChatGPT

Google API authentication: Not valid origin for the client

When making an auth request to the Google API (gapi), it's returning false on the checkOrigin.

I have removed any client id's or anything that would link directly to my account and replaced it with a regex indicating what the data is for reference.

Url: https://accounts.google.com/o/oauth2/iframerpc?action=checkOrigin&origin=https%3A%2F%2Flocal.tools&client_id=(\d{21})

My origin url is a local url, which is https://local.tools

Result: {valid: false}

I'm using the example found here without deviation (except for replacing clientid with my 21 digit clientid): https://ga-dev-tools.appspot.com/embed-api/third-party-visualizations/

The items I'm trying to display show up nicely on the demo site, but aren't getting past the Not valid origin for the client error on my local.tools site.

and "local.tools" is deffo configured in the API console and your local server is running on port 80? Try editing your /etc/hosts to point your demo site URL to 127.0.0.1 and see what happens.
Nothing good would happen because my development environment is not on 127.0.0.1. It also doesn't matter what port it's running on. It can be used on other ports besides port 80.
Google API authentication: Not valid origin for the client. --- It might be in case, while you are using same email id for creating client id and for sign-in through webpage

i
iamcootis

Clear your browser cache. Started getting this error in Chrome and then I created a new client id and was still getting the issue. Opened firefox and it worked, so I cleared the cache on Chrome and it started working.


Cannot believe cache plays that much role in it.
I had issue like this in iOS safari. The origin was accepted in chrome on iOS but not updated in safari. I’m hoping it will time out sooner or later.
Or go take a break for an hour or so. Especially if on a device where you don’t want to clear all cache or on mobile where it’s harder to do so.
Amazingly enough, clearing the browser cache actually solved this issue for me as well...
Perfect! It works for me after clearing the cache, Thanks a lot.
d
designdit

I received the same console error message when working with this example: https://developers.google.com/analytics/devguides/reporting/embed/v1/getting-started

The documentation says not to overlook two critical steps ("As you go through the instructions, it's important that you not overlook these two critical steps: Enable the Analytics API [&] Set the correct origins"), but does not clearly state WHERE to set the correct origins.

Since the client ID I had was not working, I created a new project and a new client ID. The new project may not have been necessary, but I'm retaining (and using) it.

Here's what worked:

Create a new project

Add and Enable the Analytics API

Create a new credential - ensure that it is an OAUTH credential (scroll to the bottom of this page for instructions https://developers.google.com/api-client-library/javascript/start/start-js#Setup).

During creation of the credentials, you will see a section called "Restrictions Enter JavaScript origins, redirect URIs, or both". This is where you can enter your origins.

Save and copy your client ID (and secret).

My script worked after I created the new OAUTH credential, assigned the origin, and used the newly generated client ID following this process.


Yes, that was what I needed. Thank you! Seems like it should be obvious now, but the docs are a bit hard to follow.
if I test with a localhost, how can I add the oauth there? for example If I need to let access to localhost:9000/en/login
You don't need to worry about the oAuth callback. When you use the example given, there is a little button that says something along the lines of "google Authenticate" or I don't remember. But it's there and you click on it and a popup window comes up to authenticate. Adding my domain to the origins is what I needed to do, including after uploading the example to the server. I had to add the server URL to the origin domains.
My script worked with new credential in the old app. I believe this is a bug from Google, because I did try adding my origin to my old credential.
Had the same issue and these steps worked for me! The trick was Enabling the Analytics API before creating the Credentials
S
Salman Saleem

try clear caches and then hard reload, i had same error but when i tried to run on incognito browser in chrome it worked.


Worth noting a normal hard refresh with a shift f5 or ctrl f5 didn't do the trick on chrome until I pressed F12 to open the console tab, then right-clicking on the Refresh and click on "Empty cache and Hard Reload"
T
TitanFighter

Credentials do not work if API is not enabled. In my case the next steps were needed:

Go to https://console.developers.google.com/apis/library Enter 'People' From the result choose 'Google People API' Click 'Enable'


This saved my day ..!!! The error seems totally unrelated but this worked
After many hours this solve my problem.
w
walter

Key Point: Add both http://localhost and http://localhost:port_number to the Authorized JavaScript origins box for local tests or development.


Hi - where do you set this?
This worked for me. I had originally only listed localhost:port. Once I added localhost (without the port) alongside the one with port, it immediately started working.
Yes! Add BOTH localhost and localhost:<port>
s
sudharsan tk

Creating new oauth credentials worked for me


Before I looked in SO I thought "maybe it's the cache".. after deleting the cache didn't work, This solution did.
your solution worked for me. kind of weird from google
Even I created a new client Id and it worked for me
R
Reece

For me - I just went here:

https://console.developers.google.com/apis/credentials

Then chose the right project; then choose the credential with the same ID shown in your console error message. When editing the credentials you can add multiple origins to the white list.


Nice, this is the correct answer for new client address
e
ejlp12

You probably use Client ID like this: .apps.google.com

Make sure your client ID is without ".apps.google.com"


This worked for me. I think the reason it works is that the original URL that has client_id=<CLIENT_ID>.apps.google.com in the query params is cached by Google for quite a while, whereas the one with the updated ClientID is not (even though the two should be the same).
This is the only solution here that worked for me.
In my case i had to remove ".apps.googleusercontent.com" and magically it started working.
Thank you, you saved me a lot of time!
B
Binh Ho

After updated Authorized JavaScript origins browser still caching old data, so I need to Empty cache and hard reload then it works

1. Change Authorized origins

https://i.stack.imgur.com/3Js2p.png

2. Open Dev Tool (F12) then right-click into reload button

https://i.stack.imgur.com/menLn.png


S
Sandy

Clearing the cache on chrome works!

Please find the steps below to clear the cache.

Open dev tools (Right-click on the page and select inspect/ press F12) Right-click on the chrome reload button while the dev tool is opened. (You will find the option to clear the cache and reload the site)


N
Netch

clearing the cache works for me.

for React developers try to restart the project otherwise it will show the same error again and again.


D
David

It was a referrer-policy problem.

This has been such a pain for a long time to me too...

Found the issue, my website instance had a referrer policy set to no-referrer. After setting it to no-referrer-when-downgrade, the One Tap prompt showed up as expected.

https://stackoverflow.com/a/63039142/15565029

If you are using Django, SECURE_REFERRER_POLICY is 'same-origin' by default. Change it by adding the below code in your settings file.

# settings.py
SECURE_REFERRER_POLICY = 'no-referrer-when-downgrade' 

https://docs.djangoproject.com/en/3.2/ref/settings/#std:setting-SECURE_REFERRER_POLICY


L
Lee Chee Kiam

Similar to few answers at above but with screenshots. If you created project for Firebase, may also use the same steps to configure at Google Cloud Platform console.

Select the project at https://console.cloud.google.com/ Navigate to Credentials Click Edit button for the related OAuth 2.0 Client ID Add URI into Authorized JavaScript origins Don't forget to Save

https://i.stack.imgur.com/jxUNE.png

https://i.stack.imgur.com/ArTQ8.png


M
Murat Demirsoy

That worked for me after trying for an hour:

On https://console.cloud.google.com/apis/credentials :

Edit Client Outh (mine was: Web Client (Auto Created by Google Service), which was created by my Firebase Web Project)

Enter JavaScript Origin for the Client ID (mine was: localhost:NNNN) and don't forget to Save.

Try google login for half an hour: didn't work

Enabled Google Analytics as suggested above

Empty Chrome cache and hard reload as suggested above

Try google login for half an hour: didn't work

Sign Out from https://console.cloud.google.com, and sign in again

Empty Chrome cache and hard reload

Now it worked

I don't know which one of the above fixed the problem. May be it was just a matter of time for cloud.google to recognize my new JavaScript Origin.


D
Der_Meister

I got the error because of Allow-Control-Allow-Origin: * browser extension.


s
sandy

Trying on a different browser(chrome) worked for me and clearing cache on firefox cleared the issue.

(PS: Not add the hosting URIs to Authorized JavaScript origins in API credentials would give you Error:redirect_uri_mismatch)


V
VardanMelkonyan

If you are running it on localhost change the port to 5000 and it will fix it


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now